rory.ap
rory.ap

Reputation: 35318

Regular expression that excludes carriage return returns a match with carriage return

Consider the following C# code:

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;

or this in 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

And the contents of C:\_Temporary\MyFile.txt:

"First" -- here is my first item.

"Second" -- here is my second item.

"Third" -- here is my third item.

If the RegEx meta-character . excludes all line-ending characters (and, indeed, is preventing the match from returning the rest of the contents below "Second" -- here is my second item.), why is it that val ends with a carriage return (\r)?

enter image description here

Upvotes: 1

Views: 845

Answers (1)

ryanyuyu
ryanyuyu

Reputation: 6486

According to Microsoft's .NET 4.5 Regex, the . is a "Wildcard: Matches any single character except \n.

Upvotes: 5

Related Questions