Does the .NET regex engine support built-in mode modifiers?

The C # application used by my company takes regular expression strings from a database table and matches them in different text files. The problem is that the application does not have the default RegexOptions parameter set, and I need to use the "Point corresponds to a new line" mode.

Does the engine support built-in mode modifiers the same way

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

or "global" as in PHP

 "/A(.*?)B/s" 
+4
source share
3 answers

Yes. See here .

 (?s:) 

Enabling single line mode.

+1
source

Besides the flags of the RegexOptions compiler, there is no direct equivalent for the /s modifier style, but you can get the same effect by placing the built-in modifier at the very beginning of your regular expression:

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

Remember that there are two forms of the built-in modifier. The one you used in your example:

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

... does not have a colon. (?s) is just a switch that turns on DOTALL mode until (?-s) turns it off again. Another version with a colon is actually not an exciting group (?:...) with a built-in mode switch. The mode switch is only effective when part of the regular expression within this group is in control. Using this version, your example will become

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

... or if you still want to use a capture group:

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

You do not want to mix the two versions. If you were to write your original regular expression as follows:

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

... it will not throw an exception, but it will not work as intended. (?s:) there will simply be nothing in DOTALL mode, but (?-s:) will no longer correspond in DOTALL mode.

+3
source

I am sure that it (supports built-in modifiers)!

Just use the mode modifier (?s:) to enable the "SingleLine" mode, which makes the dot character match the newline characters.

To test how a regular expression will work in the .NET flavor, I find it convenient to use the RAD Regex Designer , which uses .NET Regex.

+2
source

All Articles