RegexOptions.IgnorePatternWhitespace and group naming, doesn't allow spaces after parentheses?

Take a look at the following code that you can check in LINQPad if you want (or ideone ):

void Main()
{
    var options = RegexOptions.IgnorePatternWhitespace;
    var reWorks       = new Regex(@"(?<a>)", options);
    var reDoesNotWork = new Regex(@"( ?<a>)", options);
}

I would think that the RegexOptions.IgnorePatternWhitespace option should allow both to work:

IgnorePatternWhitespace
Eliminates unreleased empty space from the template and includes comments marked with #. However, the value of IgnorePatternWhitespace does not affect or eliminate the gap in character classes.

(my emphasis is to point out that part which, it seems to me, should cover this case)

However, it seems that the group naming (and possibly other sequences) really should be adjacent to the group opening bracket, and thus I get this exception:

ArgumentException
parsing "(?)" Is the quantifier {x, y} following it.

Is it an error in the documentation, an error in the code (i.e. implementation Regex), or is there something else that I misunderstand here?

I discovered this when I tried to write the following regular expression code to document the regular expression for future maintainers:

var Regex = new Regex(@"
    ...
    (
       ?<var1>          # group for first variable
       ...
    )

and I had to change it to this:

var Regex = new Regex(@"
    ...
    (?<var1>            # group for first variable
       ...
    )
+4
source share
1 answer

, , .

...

Regex .

, : A, [, ), ^.

, , \w, , [a-zA-Z0-9_], .

. , , , - , {1,200}, .

Regex Engine, , , , , - :

String.Replace(Input, WhiteSpace, "")

, , TWICE :

ABCD[ ]EFGH\ IJKL

, IgnoreWhiteSpace " " , .

, : .{ 2,3} , ... : " !" - . ? !!! .

: ( ?>Blah). (, Regex Engine , , Sub-Expression WhiteSpace. !, . : WhiteSpace. WhiteSpace. : QuestionMark. !... , , , -... wtf? BREAK

Long Story Short: Token Structures WhiteSpace. White-Space , , . , Parser Token, .

, - String.Replace(Input, WhiteSpace, "") Regex. .

+1

All Articles