I have a regex ( "(<lof<).*?(>>)"
) that works and matches perfectly on single line input. However, if the input contains newlines between the two () parts it does not match at all. What's the best way to ignore any newlines at all in that case?
views:
47answers:
2
+4
A:
Create your regular expression object with the RegexOptions.Singleline
option enabled:
Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).
Andrew Hare
2010-05-06 15:32:53
+1 Alternatively, though probably not as fast, you can use `[\s\S]` in place of `.` This would allow matching across some newlines and still using the dot normally in the same regex.
Tomalak
2010-05-06 15:45:24
@Tomalak: There's also the inline-flags notation: `<lof<(?s:.*?)>>` - Within those parens the dot would match linefeeds; elsewhere it wouldn't.
Alan Moore
2010-05-06 19:38:53
@Alan: True, I keep forgetting about them. :)
Tomalak
2010-05-06 20:12:56
A:
You can use RegexOptions.Singleline
Regex.Match(subjectString, "regex here", RegexOptions.Singleline)
Michael
2010-05-06 15:33:31