我有以下字符串:
~header1
content1
content2
content3
content4
~header2
content5
content6
content7
content8
~header3
content9
content10
content11
content12
~header4
我需要使用C#语言中的正则表达式提取每个标头(~header
)之间的内容。怎么做到这一点?
我尝试使用以下代码:
string pattern = "\\~.*\\~";
MatchCollection matches = Regex.Matches(data , pattern, RegexOptions.Multiline ) ;
但没有得到任何输出。
因此会有一个数组作为输出,假设其名称为m
及其内容:
m[0] = "~header1|content1|content2|content3|content4";
m[1] = "~header2|content5|content6|content7|content8";
etc
答案 0 :(得分:1)
您可以在此处使用lookarounds
。
(?<=~header\d+)[\s\S]*?(?=~header\d+)
string strRegex = @"(?<=~header\d+)[\s\S]*?(?=~header\d+)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"~header1" + "\n" + @"content1" + "\n" + @"content2" + "\n" + @"content3" + "\n" + @"content4" + "\n" + @"~header2" + "\n" + @"content5" + "\n" + @"content6" + "\n" + @"content7" + "\n" + @"content8" + "\n" + @"~header3" + "\n" + @"content9" + "\n" + @"content10" + "\n" + @"content11" + "\n" + @"content12" + "\n" + @"~header4";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
答案 1 :(得分:1)
这将为您解决问题
string[] m = Regex.Split(value1, @"(?=~header)").Where(x => !string.IsNullOrEmpty(x)).ToArray();
答案 2 :(得分:1)