Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Grouping Constructs ใน Regular Expression ของ C#

มีหมวดหมู่ตัวอักษร ตัวดำเนินการ โครงสร้างต่างๆ สำหรับกำหนด Regular Expressions หนึ่งในนั้นคือ Grouping Constructs อธิบาย sub-expressions และจับ substring ของ input string

ตาราง Grouping Constructs

Grouping constructคำอธิบายPatternMatches
( subexpression )จับ subexpression ที่ตรงกัน ให้หมายเลขลำดับ 0-based(\w)\1"ee" ใน "deep"
(?<name>subexpression)จับ subexpression ใส่กลุ่มชื่อ name(?<double>\w)\k<double>"ee" ใน "deep"
(?<name1-name2>subexpression)นิยาม balancing group(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*(?(Open)(?!))$"((1-3)*(3-1))" ใน "3+2^((1-3)*(3-1))"
(?: subexpression)นิยาม non-capturing groupWrite(?:Line)?"WriteLine" ใน "Console.WriteLine()"
(?imnsx-imnsx:subexpression)เปิด/ปิดตัวเลือกใน subexpressionA\d{2}(?i:\w+)\b"A12xl", "A12XL" ใน "A12xl A12XL a12xl"
(?= subexpression)Zero-width positive lookahead assertion\w+(?=\.)"is", "ran", "out" ใน "He is. The dog ran. The sun is out."
(?! subexpression)Zero-width negative lookahead assertion\b(?!un)\w+\b"sure", "used" ใน "unsure sure unity used"
(?<= subexpression)Zero-width positive lookbehind assertion(?<=19)\d{2}\b"51", "03" ใน "1851 1999 1950 1905 2003"
(?<! subexpression)Zero-width negative lookbehind assertion(?<!19)\d{2}\b"ends", "ender" ใน "end sends endure lender"
(?> subexpression)Nonbacktracking (greedy) subexpression[13579](?>A+B+)"1ABB", "3ABB", "5AB" ใน "1ABB 3ABBC 5AB 5AC"