jollarvia
jollarvia

Reputation: 379

Regex to exclude from second capture group if matches certain criteria

My regex so far is:

^(UCX_|UBX_|USP_)([A-Za-z0-9]\w+)(_\d+)?$

When I test the string:

UCX_1maxi_holiday2_blah_343

It correctly tests true however my last capture group will never be reached because it matches the second group also. i.e. if the last underscore demarcated section is all numbers I want that section in the third capture group not the second. Else I want it in the second group.

I've thought of lookaheads and -behinds and I don't think they will work for this but I could be surprised.

I could do a separate test to check the end of the string alone, then split the strings as I need but intellectual curiosity.

I'm wondering if there is a way to amend the second capture group to stop it from clobbering the end if it's all numbers

Anyone's got some Regex JiuJitsu for this?

Upvotes: 0

Views: 155

Answers (2)

Marcel
Marcel

Reputation: 3258

You can simplify doing that:

^(UCX_|UBX_|USP_)(\w+?)(_\d+)?$

\w character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.

Upvotes: 0

cco
cco

Reputation: 6281

If you change the second capture to non-greedy, you'll get what you want. Use:

^(UCX_|UBX_|USP_)([A-Za-z0-9]\w+?)(_\d+)?$

Upvotes: 1

Related Questions