簡體   English   中英

這個正則表達式意味着什么

[英]What does this regex mean

我真正需要知道的是:

  1. 什么(?(意思是?
  2. 什么?:是什么意思?

我想弄清楚的正則表達式是:

(注意以下正則表達式中的上述符號)

(?(?=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] 這些正則表達式唯一不同的是形成的組的數量。

因此,我的正則表達式將組成一個組,其中包含andblah

你的正則表達式不會組成任何組。只有當它與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

(?:...)

是一個非捕獲 它的工作方式與(...)類似,但它不會創建反向引用( \\1等)以供以后重復使用。

(?(condition)true|else)

有條件的 ,其嘗試匹配condition ; 如果成功,它將嘗試匹配true ,否則,它將嘗試匹配else

這是一個很少見的正則表達式構造,因為它沒有太多的用例。 在你的情況下,

(?(?=and )(and )|(blah))

可以改寫為

(and |blah)

?:是非捕獲組。 (?ifthen|else)用於構造if,then表達式。

您可以在這里閱讀更多相關信息。

http://www.regular-expressions.info/conditional.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM