正则表达式,匹配2个单词(包括)并包含到下一个新行

时间:2018-04-23 07:05:03

标签: c# regex

我尝试替换以下数据,从Item Id:到包含end:的最后一行的末尾

Some junk
Item Id: 401421502995 
end: blah blah blag, Item Id: 401343012211
end: blah blah blah blah basdfasd
Some other junk

所以在本质上所有这些东西都将被替换

Item Id: 401421502995 
end: blah blah blag, Item Id: 401343012211
end: blah blah blah blah basdfasd

离开我

Some junk
Some other junk

这有多种配置,但行end:需要进行

这里有我所拥有的,它与end:匹配,但不会继续到行尾

(Item Id:).*?(end:)

我该怎么做包括\n我的大脑被击中

1 个答案:

答案 0 :(得分:2)

您可以使用

Item Id:(?s:.*?)end:(?:(?!Item Id:).)*\n?

请参阅regex demo

<强>详情

  • Item Id: - 文字子字符串
  • (?s:.*?) - 启用了RegexOptions.Singleline选项的修改器组
  • end: - 文字子字符串
  • (?:(?!Item Id:).)* - 0次出现任何char,但LF不会启动Item Id: char序列(请参阅more details on this construct here
  • \n? - 一个可选的LF字符(可选择匹配输入中的最后一行)。