Grails/Groovy正则表达式-如何使用(?i)使所有内容不区分大小写? [英] Grails/Groovy regular expression- how to use (?i) to make everything case insensitive?

查看:251
本文介绍了Grails/Groovy正则表达式-如何使用(?i)使所有内容不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下RegEx:

I use the following RegEx:

url (blank:false, matches: /^(https?:\/\/)(?:[A-Za-z0-9]+([\-\.][A-Za-z0-9]+)*\.)+[A-Za-z]{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/)

我想添加(?i)以使所有内容均不区分大小写.我应该如何添加呢?

I want to add (?i) to make everything case insensitive. How should I add this?

推荐答案

我可以确认正则表达式开头的(?i)使其不区分大小写.

I can confirm the (?i) at the beginning of the regex makes it case insensitive.

无论如何,如果您的目的是减少正则表达式的长度,则可以使用groovy 美元斜线形式.它允许您不转义斜杠/(转义字符变为$).

Anyway, if your purpose is to reduce the regex length you can use the groovy dollar slashy string form. It allows you to not escape slashes / (the escape char becomes $).

此外:

  • POSIX字符\p{Alnum}[0-9a-zA-Z]的紧凑等效项(通过这种方式,您可以完全避免使用(?i)).

  • the POSIX chars \p{Alnum} is the compact equivalent of [0-9a-zA-Z] (this way you can avoid to use the (?i) at all).

从char类[\-\.]->中删除不需要的反斜杠. [-.](当破折号是第一个或最后一个元素,并且点在字符组中始终是文字时,则不是强制性的.)

remove unneeded backslashed dash from char class [\-\.] -> [-.] (it's not mandatory when the dash is the first or the last element and also the dot is always literal inside a character group).

从协议部分中删除不需要的圆括号

remove unneeded round brackets from the protocol section

在以下版本中,我利用美元斜线字符串和自由行正则表达式标志(?x)的多行支持:

In the following version I take advantage of the multiline support of dollar slashy string and the free-spacing regex flag (?x):

$/(?x)
  ^                      # start of the string
  https?://              # http:// or https://, no need of round brackets
  (                      # start group 1, have to be a non capturing (?: ... ) but is less readable
    \p{Alnum}+           # one or more alphanumeric char instead of [a-zA-Z0-9]
    ([.-]\p{Alnum}+)*    # zero or more of (literal dot or dash followed by one or more [a-zA-Z0-9])
    \.                   # a literal dot
  )+                     # repeat the group 1 one or more
  \p{Alpha}{2,40}        # between 2 and 40 alphabetic chars [a-zA-Z]
  (:[1-9][0-9]{0,4})?    # [optional] a literal colon ':' followed by at least one non zero digit till 5 digits
  (/\S*)?                # [optional] a literal slash '/' followed by zero or more non-space chars
/$

一刀切的精简版:

$/^https?://(\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}([1-9][0-9]{0,4})?(/\S*)?/$

如果必须使用斜杠版本,则等效:

If you must use the slashy version this is an equivalent:

/^https?:\/\/(?:\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/

测试所有这些正则表达式的代码片段:

A snippet of code to test all these regex:

def multiline_pattern = $/(?x)
  ^                      # start of the string
  https?://              # http:// or https://, no need of round bracket
  (                      # start group 1, have to be a non capturing (?: ... ) but is less readable
    \p{Alnum}+           # one or more alphanumeric char, instead of [a-zA-Z0-9]
    ([.-]\p{Alnum}+)*    # zero or more of (literal dot or dash followed by one or more [0-9a-zA-Z])
    \.                   # a literal dot
  )+                     # repeat the group 1 one or more
  \p{Alpha}{2,40}        # between 2 and 40 alphabetic chars [a-zA-Z]
  (:[1-9][0-9]{0,4})?    # [optional] a literal colon ':' followed by at least one non zero digit till 5 digits
  (/\S*)?                # [optional] a literal slash '/' followed by zero or more non-space chars
/$

def compact_pattern = $/^https?://(\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(/\S*)?/$

def slashy_pattern  = /^https?:\/\/(?:\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/

def url1    = 'https://www.example-test.domain.com:12344/aloha/index.html'
def notUrl1 = 'htxps://www.example-test.domain.com:12344/aloha/index.html'
def notUrl2 = 'https://www.example-test.domain.com:02344/aloha/index.html'

assert url1 ==~ multiline_pattern
assert url1 ==~ compact_pattern
assert url1 ==~ slashy_pattern

assert !( notUrl1 ==~ compact_pattern )
assert !( notUrl1 ==~ slashy_pattern  )
assert !( notUrl1 ==~ slashy_pattern  )

assert !( notUrl2 ==~ compact_pattern )
assert !( notUrl2 ==~ slashy_pattern  )
assert !( notUrl2 ==~ slashy_pattern  )

这篇关于Grails/Groovy正则表达式-如何使用(?i)使所有内容不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆