Reputation: 67
In following code, why is the pattern not matched by the regular expression, but it is properly working if I am reading the 12sep.txt file line by line using regular expression?
string file = @"C:\Documents and Settings\Sandeep.kumar\Desktop\12sep.txt";
string filedta = File.ReadAllText(file);
string pattern = @"^[^\s]+.[^\s]txt$";
Regex rx = new Regex(pattern, RegexOptions.None);
MatchCollection mc = rx.Matches(filedta);
Upvotes: 1
Views: 1271
Reputation: 1487
Regex special character "^" and "$" don't have the same meaning when the input string is single-line or multi-line. In single line they mean "string start" and "string end". In multi-line they mean "line start" and "line end".
You have an option in RegexOptions to control this : RegexOptions.Multiline
And the documentation clearly states what it does : "Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string."
from http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
Upvotes: 11