我有这样的内容:
var testInput =
"05(testcontent)\r\n" +
"06(testcontent2)\r\n" +
"07(testcontent3)(testcontent4)" +
"08(testcontent5)";
我需要为每一行获取一个代码字符串和两个值字符串。对于第一行:
- 代码:
"05"
- 价值1:
"testcontent"
- 值 2:空字符串。
对于第三行:
- 代码:
"07"
- 价值1:
"testcontent3"
- 价值2:
"testcontent4"
我使用的模式:
// (?<Code>[0-9]{2}) - 2 digit number
// \((?<Value1>.+)\) - First value, which is inside the parentheses.
// (\((?<Value2>.+)\))? - Second value, which also is inside the parentheses.
// The second value does not always exist. Which is why it has "?" at its end.
var testPattern = @"(?<Code>[0-9]{2})\((?<Value1>.+)\)(\((?<Value2>.+)\))?";
我使用的代码:
var testRegex = new Regex(testPattern,
RegexOptions.Compiled |
RegexOptions.CultureInvariant |
RegexOptions.ExplicitCapture |
RegexOptions.Multiline);
foreach (Match match in testRegex.Matches(testInput))
Console.WriteLine("{0}: {1} | {2}",
match.Groups["Code"].Value,
match.Groups["Value1"].Value,
match.Groups["Value2"].Value);
我得到的结果:
05: testcontent |
06: testcontent2 |
07: testcontent3)(testcontent4)08(testcontent5 |
如果我^
在模式的开头和$
结尾使用,我会变得更糟:
07: testcontent3)(testcontent4)08(testcontent5 |
所以,
- 为什么当我指定“RegexOptions.Multiline”时
^
,$
事情变得更加复杂? - 我的模式有什么问题?