Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with non-capturing groups in regular expression

Tags:

regex

I'm attempting to capture the 6 digit number in the following:

ObjectID: !nrtdms:0:!session:slonwswtest1:!database:TEST:!folder:ordinary,486150:

I tried the following regex:

\d+(?::$)

attempting to use a non-capturing group to strip the colon out of the returned match, but it returns the colon as in:

486150:

Any ideas what I'm doing wrong?

like image 258
CodingUnderDuress Avatar asked Jan 19 '26 11:01

CodingUnderDuress


2 Answers

You want a positive lookahead:

\d+(?=:$)

A non-capturing group is simply a group that cannot be accessed via a backreference; they still are part of the match, nonetheless.

Alternatively, you can use

(\d+):$

and obtain the 1st match group.

like image 123
arshajii Avatar answered Jan 22 '26 16:01

arshajii


You should use a positive lookahead rather than a non-capturing group

\d+(?=:$)
like image 44
iruvar Avatar answered Jan 22 '26 16:01

iruvar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!