当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

C# 正则表达式

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-14 1:48:22

描述模式匹配*匹配上一个元素零次或多次。\d*\.\d".0"、 "19.9"、 "219.9"+匹配上一个元素一次或多次。"be+""been" 中的 "bee", "bent" 中的 "be"?匹配上一个元素零次或一次。"rai?n""ran"、 "rain"{ n }匹配上一个元素恰好 n 次。",\d{3}""1,043.6" 中的 ",043", "9,876,543,210" 中的 ",876"、 ",543" 和 ",210"{ n ,}匹配上一个元素至少 n 次。"\d{2,}""166"、 "29"、 "1930"{ n , m }匹配上一个元素至少 n 次,但不多于 m 次。"\d{3,5}""166", "17668","193024" 中的 "19302"*?匹配上一个元素零次或多次,但次数尽可能少。\d*?\.\d".0"、 "19.9"、 "219.9"+?匹配上一个元素一次或多次,但次数尽可能少。"be+?""been" 中的 "be", "bent" 中的 "be"??匹配上一个元素零次或一次,但次数尽可能少。"rai??n""ran"、 "rain"{ n }?匹配前导元素恰好 n 次。",\d{3}?""1,043.6" 中的 ",043", "9,876,543,210" 中的 ",876"、 ",543" 和 ",210"{ n ,}?匹配上一个元素至少 n 次,但次数尽可能少。"\d{2,}?""166"、 "29" 和 "1930"{ n , m }?匹配上一个元素的次数介于 n 和 m 之间,但次数尽可能少。"\d{3,5}?""166", "17668","193024" 中的 "193" 和 "024"反向引用构造反向引用允许在同一正则表达式中随后标识以前匹配的子表达式。下表列出了反向引用构造:反向引用构造描述模式匹配\ number反向引用。 匹配编号子表达式的值。(\w)\1"seek" 中的 "ee"\k命名反向引用。 匹配命名表达式的值。(?\w)\k"seek" 中的 "ee"备用构造备用构造用于修改正则表达式以启用 either/or 匹配。下表列出了备用构造:备用构造描述模式匹配|匹配以竖线 (|) 字符分隔的任何一个元素。th(e|is|at)"this is the day. " 中的 "the" 和 "this"(?( expression )yes | no )如果正则表达式模式由 expression 匹配指定,则匹配 yes;否则匹配可选的 no 部分。 expression 被解释为零宽度断言。(?(A)A\d{2}\b|\b\d{3}\b)"A10 C103 910" 中的 "A10" 和 "910"(?( name )yes | no )如果 name 或已命名或已编号的捕获组具有匹配,则匹配 yes;否则匹配可选的 no。(?")?(?(quoted).+?"|\S+\s)"Dogs.jpg "Yiska playing.jpg"" 中的 Dogs.jpg 和 "Yiska playing.jpg"替换替换是替换模式中使用的正则表达式。下表列出了用于替换的字符:字符描述模式替换模式输入字符串结果字符串$number替换按组 number 匹配的子字符串。\b(\w+)(\s)(\w+)\b$3$2$1"one two""two one"${name}替换按命名组 name 匹配的子字符串。\b(?\w+)(\s)(?\w+)\b${word2} ${word1}"one two""two one"$$替换字符"$"。\b(\d+)\s?USD$$$1"103 USD""$103"$&替换整个匹配项的一个副本。(\$*(\d*(\.+\d+)?){1})**$&"$1.30""**$1.30**"$`替换匹配前的输入字符串的所有文本。B+$`"AABBCC""AAAACC"$'替换匹配后的输入字符串的所有文本。B+$'"AABBCC""AACCCC"$+替换最后捕获的组。B+(C+)$+"AABBCCDD"AACCDD$_替换整个输入字符串。B+$_"AABBCC""AAAABBCCCC"杂项构造下表列出了各种杂项构造:构造描述实例(?imnsx-imnsx)在模式中间对诸如不区分大小写这样的选项进行设置或禁用。\bA(?i)b\w+\b 匹配 "ABA Able Act" 中的 "ABA" 和 "Able"(?#注释)内联注释。该注释在第一个右括号处终止。\bA(?#匹配以A开头的单词)\w+\b# [行尾]该注释以非转义的 # 开头,并继续到行的结尾。(?x)\bA\w+\b#匹配以 A 开头的单词Regex 类Regex 类用于表示一个正则表达式。下表列出了 Regex 类中一些常用的方法:序号方法 & 描述1public bool IsMatch(string input)指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项。2public bool IsMatch(string input,int startat)指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项,从字符串中指定的开始位置开始。3public static bool IsMatch(string input,string pattern)指示指定的正则表达式是否在指定的输入字符串中找到匹配项。4public MatchCollection Matches(string input)在指定的输入字符串中搜索正则表达式的所有匹配项。5public string Replace(string input,string replacement)在指定的输入字符串中,把所有匹配正则表达式模式的所有匹配的字符串替换为指定的替换字符串。6public string[] Split(string input)把输入字符串分割为子字符串数组,根据在 Regex 构造函数中指定的正则表达式模式定义的位置进行分割。如需了解 Regex 类的完整的属性列表,请参阅微软的 C# 文档。实例 1下面的实例匹配了以 'S' 开头的单词:using System;using System.Text.RegularExpressions;namespace RegExApplication{ class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "A Thousand Splendid Suns"; Console.WriteLine("Matching words that start with 'S': "); showMatch(str, @"\bS\S*"); Console.ReadKey(); } }}当上面的代码被编译和执行时,它会产生下列结果:Matching words that start with 'S':The Expression: \bS\S*SplendidSuns实例 2下面的实例匹配了以 'm' 开头以 'e' 结尾的单词:using System;using System.Text.RegularExpressions;namespace RegExApplication{ class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "make maze and manage to measure it"; Console.WriteLine("Matching words start with 'm' and ends with 'e':"); showMatch(str, @"\bm\S*e\b");

上一页  [1] [2] [3]  下一页


C# 正则表达式