C#分組構造

2019-10-16 23:17:29

分組構造描述正規表示式的子表示式並捕獲輸入字串的子字串。下表列出了分組結構:

分組結構 描述 模式 匹配
( subexpression ) 捕獲匹配的子表示式,並將其分配為從零開始的序數 (\w)\1 "ee"匹配"deep"
(?< name >subexpression) 將匹配的子表示式捕獲到命名組中 (?< double>\w)\k< double> "ee"匹配"deep"
(?< name1 -name2 >subexpression) 定義平衡組定義 (((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*(?(Open)(?!))$ "((1-3)*(3-1))"匹配"3+2^((1-3)*(3-1))"
(?: subexpression) 定義非捕獲組 Write(?:Line)? "WriteLine" 匹配"Console.WriteLine()"
(?imnsx-imnsx:subexpression) 在子表示式中應用或禁用指定的選項 A\d{2}(?i:\w+)\b "A12xl", "A12XL" 匹配 "A12xl A12XL a12xl"
(?= subexpression) 零寬正向前瞻斷言 \w+(?=\.) "is", "ran", 和 "out" 匹配 "He is. The dog ran. The sun is out."
(?! subexpression) 零寬度負向前瞻斷言 \b(?!un)\w+\b "sure", "used" 匹配 "unsure sure unity used"
(?< =subexpression) 零寬度正向後斷言 (?< =19)\d{2}\b "51", "03" 匹配 "1851 1999 1950 1905 2003"
(?< ! subexpression) 零寬度負向後斷言 (?< !19)\d{2}\b "ends", "ender" 匹配 "end sends endure lender"
(?> subexpression) 非追溯(或「貪懶」)子表示式 [13579](?>A+B+) "1ABB", "3ABB""5AB" 匹配 "1ABB 3ABBC 5AB 5AC"