tags:

views:

330

answers:

3

A C# application my company uses is taking regular expression strings from a database table and matching them on different text files. The problem is that the application has no RegexOptions set as default and I need to use the "Dot matches new line" mode.

Does the engine support inline mode modifiers just as like

"A(?s)(.*?)(?-s)B"

or "global" mode modifiers like in PHP

"/A(.*?)B/s"
+1  A: 

Yes. See here.

(?s:)

Should turn on single line mode.

Jeff Moser
+2  A: 

Sure it does (support inline mode modifiers)!

Just use the mode modifier (?s:) to turn on "SingleLine mode" which makes the dot character match newlines.

To check how a Regular Expression would work in the .NET flavour, I find it handy to use the RAD Regex Designer which uses the .NET Regex engine.

Cerebrus
i like more regexbuddy :)
knoopx
Yes, I do too, but then Regexbuddy supports all Regex flavours and features.
Cerebrus
+2  A: 

Aside from the RegexOptions compiler flags, there's no direct equivalent for the /s style of modifier, but you can get the same effect by placing an inline modifier at the very beginning of your regex:

"(?s)A(.*?)B"

Be aware that there are two forms of the inline modifier. The one you used in your example:

"A(?s)(.*?)(?-s)B"

...has no colon. The (?s) is merely a switch that turns DOTALL mode on until the (?-s) turns it off again. The other version, with the colon, is actually a non-capturing group, (?:...), with the mode switch built in. The mode switch has effect only while the part of the regex inside that group is in control. Using that version, your example would become

"A(?s:.*?)B"

...or, if you still wanted to use a capturing group:

"A(?s:(.*?))B"

You don't want to mix up the two versions. If you were to write your original regex like this:

"A(?s:)(.*?)(?-s:)B"

...it wouldn't throw an exception, but it wouldn't work as intended. The (?s:) would simply match nothing in DOTALL mode, and the (?-s:) would match some more nothing in not-DOTALL mode.

Alan Moore
Ahhh, the mysteries of Regex!
Cheeso