我需要知道的是:
(?(
的含义是什么??:
的含义是什么?
我正在尝试理解以下正则表达式:
(请注意上述符号在以下正则表达式中的使用)
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
我需要知道的是:
(?(
的含义是什么??:
的含义是什么?我正在尝试理解以下正则表达式:
(请注意上述符号在以下正则表达式中的使用)
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
(?(?=and )(and )|(blah))
模式类似于 if-then-else 条件语句,即 (?(expression)yes|no)
。如果存在 and
,则匹配and
,否则匹配blah
。
(?:)
是一个非捕获组,它不会被包含在组中或用作反向引用\1。
因此,
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
and 1900
blah2000
and 2012
blah2013
注意(这一切都与分组有关)
可以使用以下正则表达式实现相同的效果
(and |blah)(?:[1][9]|[2][0])[0-9][0-9]
。
这些正则表达式之间唯一的区别在于所形成的组数。
因此,我的正则表达式将形成一个包含 and
或 blah
的组。
你的正则表达式不会形成任何组。只有当匹配到blah
时它才会形成一组。
. Any character except newline.
\. A period (and so on for \*, \(, \\, etc.)
^ The start of the string.
$ The end of the string.
\d,\w,\s A digit, word character [A-Za-z0-9_], or whitespace.
\D,\W,\S Anything except a digit, word character, or whitespace.
[abc] Character a, b, or c.
[a-z] a through z.
[^abc] Any character except a, b, or c.
aa|bb Either aa or bb.
? Zero or one of the preceding element.
* Zero or more of the preceding element.
+ One or more of the preceding element.
{n} Exactly n of the preceding element.
{n,} n or more of the preceding element.
{m,n} Between m and n of the preceding element.
??,*?,+?,
{n}?, etc. Same as above, but as few as possible.
(expr) Capture expr for use with \1, etc.
(?:expr) Non-capturing group.
(?=expr) Followed by expr.
(?!expr) Not followed by expr.
表达式(?(?=and )(and )|(blah))
是一个if-else
表达式。
您可以在此处测试正则表达式:Regexpal.com
?:
是一个非捕获组。
(?ifthen|else)
用于构建 if、then 表达式。
您可以在此处阅读更多相关信息。