I want to match text between word1 and first occurrence of word2. What's the best way to do that, considering that text may include newline characters? Is there a pattern like this: (word1)(not word2)*(word2)?
问问题
71 次
2 回答
3
于 2012-09-30T18:41:49.633 回答
1
您可以使用 SingleLine 选项匹配它们:
//use '*' or '*?' depending on what you want for "word1 aaa word2 bbb word2"
string pattern = "word1(.*)word2";
var m = Regex.Match(text1, pattern, RegexOptions.Singleline);
Console.WriteLine(m.Groups[1]); // the result
MSDN 关于 SingleLine :
... 导致正则表达式引擎将输入字符串视为由单行组成。它通过更改句点 (.) 语言元素的行为来做到这一点,使其匹配每个字符,而不是匹配除换行符 \n 或 \u000A 之外的每个字符。
于 2012-09-30T18:54:37.617 回答