[英]What does this regex mean
我真正需要知道的是:
(?(
意思是? ?:
是什么意思? 我想弄清楚的正則表達式是:
(注意以下正則表達式中的上述符號)
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
(?(?=and )(and )|(blah))
模式被用於像如果-則-否則像(?(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
表達式:)
你可以在這里測試reqular表達式: Regexpal.com
?:
是非捕獲組。 (?ifthen|else)
用於構造if,then表達式。
您可以在這里閱讀更多相關信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.