[英]Regular expression that excludes carriage return returns a match with carriage return
考虑以下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
:
“第一”-这是我的第一项。
“第二个”-这是我的第二个项目。
“第三”-这是我的第三项。
如果RegEx元字符.
排除所有行尾字符(并且实际上是在阻止匹配返回"Second" -- here is my second item.
以下的其余内容"Second" -- here is my second item.
),为什么val
以回车符( \\r
)结尾?
根据Microsoft的.NET 4.5 Regex ,该.
是“通配符:匹配\\n
以外的任何单个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.