考虑以下 C# 代码:
string s = System.IO.File.ReadAllText("C:\\_Temporary\\MyFile.txt");
var reg = new System.Text.RegularExpressions.Regex("\"Second\" -- (.+)");
var val = reg.Match(s).Groups[1].Value;
或 VB.NET 中的这个:
Dim s = System.IO.File.ReadAllText("C:\_Temporary\MyFile.txt")
Dim reg = New System.Text.RegularExpressions.Regex("""Second"" -- (.+)")
Dim val = reg.Match(s).Groups(1).Value
C:\_Temporary\MyFile.txt 的内容:
"First" -- here is my first item.
"Second" -- here is my second item.
"Third" -- here is my third item.
如果 RegEx 元字符 . 排除了所有行结束字符(并且确实阻止了匹配返回 "Second"下面的其余内容——这里是我的第二项。),为什么 val 以回车 (\r) 结尾?
请您参考如下方法:
根据 Microsoft's .NET 4.5 Regex , . 是一个“通配符:匹配除 \n 之外的任何单个字符。
