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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 03:52:11

问题


I received the following exception when I was using the Regex class with the regular expression: (?'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 regular expression?


回答1:


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

From the MSDN documentation: "The string used for 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 does not matter if you use angle brackets <> or single quotes '' to indicate a group name.




回答2:


The reference for the MSDN documentation mentioned by vengafoo is here: Regular Expression Grouping Constructs

(?<name> subexpression)
Captures the matched subexpression into a group name or number name. The string used for name must not contain any punctuation and cannot begin with a number. You can use single quotes instead of angle brackets; for example, (?'name').




回答3:


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



来源:https://stackoverflow.com/questions/122951/invalid-group-name-group-names-must-begin-with-a-word-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!