其他分享
首页 > 其他分享> > 字符串匹配--Regex

字符串匹配--Regex

作者:互联网

利用Regix实现字符串匹配

Eg:匹配嵌入到[]中的字符

string pattern = Regex.Escape("[") + "(.*?)]"; 
string input = "The animal [what kind?] was visible [by whom?] from the window.";

MatchCollection matches = Regex.Matches(input, pattern);
int commentNumber = 0;
Console.WriteLine("{0} produces the following matches:", pattern);
foreach (Match match in matches)
   Console.WriteLine("   {0}: {1}", ++commentNumber, match.Value);  

// This example displays the following output:
//       \[(.*?)] produces the following matches:
//          1: [what kind?]
//          2: [by whom?]

标签:Regex,following,Console,produces,--,pattern,matches,字符串
来源: https://www.cnblogs.com/Alicia-meng/p/15185838.html