tags:

views:

47

answers:

2

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?

+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
+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
@Tomalak: There's also the inline-flags notation: `<lof<(?s:.*?)>>` - Within those parens the dot would match linefeeds; elsewhere it wouldn't.
Alan Moore
@Alan: True, I keep forgetting about them. :)
Tomalak
A: 

You can use RegexOptions.Singleline

Regex.Match(subjectString, "regex here", RegexOptions.Singleline)
Michael