【发布时间】:2018-03-12 23:41:16
【问题描述】:
示例文本:
START
This is example example example example example example example example .
END
START
This is example 1234 14 756 214 6456 5 2 4234 66 match.
END
START
This is This isThis isThis isThis isThis isThis isThis isThis is.
END
我只需要匹配“START”和“END”关键字之间且包含“match”字样的文本。
我目前有这个正则表达式:
Regex.Matches(myString, @"START(.*?match.*?)END", RegexOptions.Singleline);
它不能正常工作,因为它在匹配“END”关键字之前等待“匹配”单词出现:
This is example example example example example example example example .
END
START
This is example 1234 14 756 214 6456 5 2 4234 66 match.
如果“START”和“END”关键字之间的句子不包含“match”字样,我该如何舍弃它们?
【问题讨论】:
-
"RegexOptions.Singleline" - 考虑到您的示例输入和模式,这似乎不正确。
-
@Fildor 也许阅读docs.microsoft.com/en-us/dotnet/standard/base-types/… 可能会让您明白为什么它不像看起来那么奇怪。
-
@spender 正确,不像看起来那么奇怪……不过我还是不会在这里使用它。