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.
我公司使用的C#应用程序从数据库表中获取正则表达式字符串,并在不同的文本文件上进行匹配。问题是应用程序没有将RegexOptions设置为默认值,我需要使用“Dot matches new line”模式。
Does the engine support inline mode modifiers just as like
引擎是否支持内联模式修改器
"A(?s)(.*?)(?-s)B"
or "global" mode modifiers like in PHP
或像PHP中的“全局”模式修饰符
"/A(.*?)B/s"
3 个解决方案
#2
3
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:
除了RegexOptions编译器标志之外,没有与/ s样式的修饰符直接等效,但是你可以通过在正则表达式的最开头放置一个内联修饰符来获得相同的效果:
"(?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
......没有结肠。 (?s)只是一个开关,可以打开DOTALL模式直到(?-s)再次关闭。带冒号的另一个版本实际上是一个非捕获组(?:...),内置模式开关。模式开关仅在该组内的正则表达式部分处于控制状态时才有效。使用该版本,您的示例将成为
"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.
......它不会抛出异常,但它不会按预期工作。 (?s :)在DOTALL模式下只会匹配任何内容,而(?-s :)在非DOTALL模式下会更多地匹配。
#3
2
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.
只需使用模式修饰符(?s :)打开“SingleLine模式”,使点字符匹配换行符。
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.
为了检查正则表达式在.NET风格中的工作方式,我发现使用使用.NET Regex引擎的RAD Regex Designer非常方便。
#1
#2
3
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:
除了RegexOptions编译器标志之外,没有与/ s样式的修饰符直接等效,但是你可以通过在正则表达式的最开头放置一个内联修饰符来获得相同的效果:
"(?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
......没有结肠。 (?s)只是一个开关,可以打开DOTALL模式直到(?-s)再次关闭。带冒号的另一个版本实际上是一个非捕获组(?:...),内置模式开关。模式开关仅在该组内的正则表达式部分处于控制状态时才有效。使用该版本,您的示例将成为
"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.
......它不会抛出异常,但它不会按预期工作。 (?s :)在DOTALL模式下只会匹配任何内容,而(?-s :)在非DOTALL模式下会更多地匹配。
#3
2
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.
只需使用模式修饰符(?s :)打开“SingleLine模式”,使点字符匹配换行符。
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.
为了检查正则表达式在.NET风格中的工作方式,我发现使用使用.NET Regex引擎的RAD Regex Designer非常方便。