deadlydog
deadlydog

Reputation: 24434

Regex named capture group does not work

I'm having the weirdest problem, and I know it must be something trivial I'm overlooking. I'm writing a simple regular expression which works fine, until I try and name my capture group. For an example, you can reproduce this on http://www.regexr.com/.

Text to match:

'1.2.3.4'

Regex to use:

'(.*?)'

This works fine and it matches the version number. But when I change the regex to:

'(?<VersionNumber>.*?)'

it no longer matches and regexr says Error: Invalid quantifier for target on the first question mark. I've also confirmed the same results in my C# app. What am I doing wrong here?

This is a pretty simple example, so I'm not sure what is incorrect. I did find this post, which describes a similar problem, but the answer just provides the OP with the new regex to use, instead of actually telling him why his original regex was not valid.


UPDATE

Answer was that regexr.com no longer supports named capture groups, and I must have goofed up my regex in C# the first time around, as it worked fine when I rewrote it.

Upvotes: 0

Views: 2832

Answers (1)

dognose
dognose

Reputation: 20909

You have to take care about the language. Each Language has a different implementation how a named group is defined:

JavaScript does not support it at all.

In PHP Your example would be (?P<VersionNumber>.*?)

In C# you should be able to use either (?<VersionNumber>.*?) or (?'VersionNumber'.*?)

you can look up some more examples here: http://www.regular-expressions.info/named.html

Upvotes: 5

Related Questions