Invalid group name: group names must begin with a word character

I got the following exception when I used the Regex class with regex: (? 'Named a'asdf)

System.ArgumentException: parsing \"(?'named a'asdf)\" - Invalid group name: Group names must begin with a word character.

What is the problem with my regex?

+3
source share
3 answers

The problem is the space in the capture name. Remove the space and it works great.

From the MSDN documentation: "The string used for the name must not contain any punctuation and cannot begin with a number. You can use single quotes instead of angle brackets, for example (? 'Name')."

It doesn't matter if you use angle brackets <> or single quotes '' to indicate the name of the group.

+6
source

MSDN, vengafoo, :

(?<name> subexpression)
. . ; , (? 'name').

+4

The problem is your quotes around the name of the specified capture group. Try the line: (? <Named> asdf)

-1
source

Source: https://habr.com/ru/post/1697407/


All Articles