简体   繁体   中英

Is there a regex double-quote matching syntax that can be used without modification in C#?

The following simple regex includes four double-quotes that must be matched. I'm not attempting to come up with a solution for this particular regex but am merely using it as a general example:

\s*"Hello"\s*"world"\s*

The problem I've always encountered when writing C# code that contains regexes that must match double-quotes is the cumbersome syntax I've had to use because string literals in C# are double-quote delimited. I've used the two different techniques below, neither of which I like. Aside from the additional complexity required to butcher the original regex into acceptable C# syntax, converting that syntax back into the original regex for additional development is a real pain. Is there any form that would be equally acceptable to both the regex engine and the C# language parser?

The first hack uses escape characters to escape the backslashes and double quotes that must appear literally in the regex. I view this as the most error prone approach because you get buried in backslashes for more complex regexes:

"\\s*\"Hello\"\\s*\"world\"\\s*"

The second hack breaks the original regex into multiple pieces and concatenates them. Pieces that are string literals and contain regex backslashes are preceded by an @ character to cause the backslashes to be taken literally rather than as escape characters. I view this as more verbose but less error prone than the previous approach:

@"\s*" + '"' + "Hello" + '"' + @"\s*" + '"' + "world" + '"' + @"\s*"

@"\s*""Hello""\s*""world""\s*" gives the string \s*"Hello"\s*"world"\s* . Simply double the double quotes in an @ prepended string (AKA verbatim string) to display a double quotes

Fiddle

I have discovered that by using the hex escape sequence \x22 to represent a double quote the same regex string can be used unaltered both in my regex development application (RegexBuddy) and in a C# string literal. That is, in my development application

\s*"Hello"\s*"world"\s*

can be represented directly as

\s*\x22Hello\x22\s*\x22world\x22\s*

and in a C# string literal the same regex string it can be represented as

@"\s*\x22Hello\x22\s*\x22world\x22\s*"

The string is still cluttered but at least no changes are required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM