39193

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

Question:

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?

Answer1:

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.

Answer2:

The reference for the MSDN documentation mentioned by vengafoo is here: <a href="http://msdn.microsoft.com/en-us/library/bs2twtah.aspx" rel="nofollow">Regular Expression Grouping Constructs</a>

<blockquote>

<strong>(?<name> subexpression)</strong><br /> 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. <strong>You can use single quotes instead of angle brackets; for example, (?'name').</strong>

</blockquote>

Answer3:

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

Recommend

  • Using HTML/CSS for UI in XNA?
  • C function strchr - How to calculate the position of the character?
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • Django model inheritance, filtering models
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • Grails calculated field in SQL
  • Installed module is empty
  • Declaring variable dynamically in VB.net
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Custom Tabgroup Appcelerator
  • Django: Count of Group Elements
  • Linq Objects Group By & Sum
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Splitting given String into two variables - php
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Read text file and split every line in MSBuild
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Large data - storage and query
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • 0x202A in filename: Why?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to disable jQuery.jplayer autoplay?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time