C# 正则表达式取两个关键词之间的内容
<!---go--->qwerty
asddff
asdfff
adffgg
afhgujujikuy
……
asdsqsdw<!---over--->
里面有很多行怎么取出来呢
public void ShowStructure()
{
//要匹配的字符串
string text = "<!---go--->qwerty\r\nasddff\r\nasdfff\r\nadffgg\r\nafhguj\r\nujikuy\r\n……\r\nasdsqsdw<!---over--->";
//正则表达式
string pattern = @"<!---go--->((?:(?!<!---over--->)[\s\S])*)<!---over--->";
//使用RegexOptions.IgnoreCase枚举值表示不区分大小写
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
//使用正则表达式匹配字符串,仅返回一次匹配结果
Match m = r.Match(text);
while (m.Success)
{
//显示匹配开始处的索引值和匹配到的值
System.Console.WriteLine("Match=[" + m + "]");
CaptureCollection cc = m.Captures;
foreach (Capture c in cc)
{
Console.WriteLine("\tCapture=[" + c + "]");
}
for (int i = 0; i < m.Groups.Count; i++)
{
Group group = m.Groups[i];
System.Console.WriteLine("\t\tGroups[{0}]=[{1}]", i, group);
for (int j = 0; j < group.Captures.Count; j++)
{
Capture capture = group.Captures[j];
Console.WriteLine("\t\t\tCaptures[{0}]=[{1}]", j, capture);
}
}
//进行下一次匹配.
m = m.NextMatch();
}
}
温馨提示:内容为网友见解,仅供参考
Warning: Invalid argument supplied for foreach() in /www/wwwroot/aolonic.com/skin/templets/default/contents.html on line 45
相似回答