简体   繁体   中英

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. ie 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?

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+)?$

You can simplify doing that:

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

\\w character is a character from az, AZ, 0-9, including the _ (underscore) character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM