paul
paul

Reputation: 4487

Regex expression matches without specifying comma (,) in regex

I am taking help of this website to learn regex and I am stuck at this particular lesson. Looks like regex is wrong there. When I write (\w+\s\d+)((\,\d+)?) "text" and "capture" goes green but "result" appears wrong (cross marks).

But if Write (\w+ (\d+)) it gives below result.

your task       text        capture         result
capture text    Jan 1987    Jan 1987, 1987    ✓
capture text    May 1969    May 1969, 1969    ✓
capture text    Aug 2011    Aug 2011, 2011    ✓

Now, question is (\w+ (\d+)) doesn't show that it going to capture comma but is right answer.And, in this (\w+\s\d+)((\,\d+)?) expression I have specified but it is coming wrong, why?

Upvotes: 0

Views: 100

Answers (1)

netblognet
netblognet

Reputation: 2016

That's because the capture column tells you, what you should capture. For example: Jan 1987, 1987 means you should capture two groups. 1) Jan 1987 2) 1987

They use the comma as divider between the groups. So it's not part of the string you should capture, but just a divider to tell you where the next excepted capture group starts.

If you step to the next lesson http://regexone.com/lesson/13 my example will be much more clear. In the text column there isn't any comma (e.g. 1280x720) but in capture column you're asked for "1280, 720". So this props my theory.

Upvotes: 2

Related Questions