I'm trying to create a regular expression to match a Markdown heading that correlates to a package version number, e.g.
## 5.21.30
Currently I'm using this regex:
^##\s([0-9] \.?[0-9] \.?[0-9] )
The problem is this is also returning the ##\s
. I want to remove this, so I attempted to make it into a non-capturing group.
/(?:^##\s)([0-9] \.?[0-9] \.?[0-9] )/gsm
But this doesn't seem to work (same output as before).
Any help on what I'm misunderstanding would be appreciated.
CodePudding user response:
?:
makes a non-capturing group, but it's still a group and part of the match. You could use a lookbehind:
(?<=^##\s)([0-9] \.?[0-9] \.?[0-9] )
e.g. https://regex101.com/r/3s0p7X/1
The ?<=
means that part has to appear before the numbers, but is not a group and is not part of the match. See also lookarounds at regular-expressions.info.