Mors
Mors

Reputation: 71

regex non-capture groups

Here's my regex test string,

DDD001_1_2016_6TP2_1

I need to capture DDD001_1_2016_6TP2_1 in one capture group and omit the underscore. I've tried the below regex and even with a non-capture group I still can't capture what I want.

^(.*?)_.*?_\d{2}(\d{2}(?:_).*)_.*$

From my research it looks like its not possible to omit characters in a particular capture group via regex, it has to be done with step 2 eg. code

Any help would be greatly appreciated.

Upvotes: 3

Views: 541

Answers (3)

Anshul Rai
Anshul Rai

Reputation: 782

What @ZephyrPellerin has said is correct.

Though it isn't possible to filter out the underscore, it is still possible to capture what you need in different matching groups which can then be accessed in whatever way it is done in the language you are using.

Here's a DEMO.

The regex I've used is: (?<=_\d{2})(.+)(?=_(.+)_).

Here the part before the underscore is captured in the first parentheses and the part after is in the second one. These can then be concatenated to form one string if required.

Hope this helps.

Upvotes: 0

Jan
Jan

Reputation: 43169

You could go for:

(\d{2}_[a-z0-9]+)_\d+$

See a demo on regex101.com (mind the different modifiers!).

Upvotes: 0

zetavolt
zetavolt

Reputation: 3207

In short: You can't. A match is always consecutive, even when it contains things as zero-width assertions there is no way around matching the next character if you want to get to the one after it.

However, most regular expression matches are performed in the context of a wider programming environment, in which you could use nested capture groups, subsequent regular expressions or manual manipulation to filter the last underscore.

Upvotes: 2

Related Questions