简体   繁体   中英

How can I combine these two regex expression to one?

I'm trying to combine two patterns into 1:

string pattern_1 = @"smoke-test-app-[0-9A-Fa-f\-]{36}";
string pattern_2 = @"SMOKES-[0-9]-APP-[0-9A-Fa-f\-]{16}";

Test string for pattern_1 : smoke-test-app-f47980d7-b3e5-49eb-99b6-d7413e16a0bc

Test string for pattern_2 : SMOKES-2-APP-a9a8f59dd3c74046

One key things to understand here is the possibility to use insensitive case, since you have the same word in both uppercase and lowercase. As for the rest, it's basic OR operaiton.

/smoke(s?)-(test|\d{1})-app-([0-9A-Fa-f\-]{36}|[0-9A-Fa-f\-]{16})/gmi

Note that the i flag is important here.

I've created a regex101 if you wish to test more cases

PS
I did this out of pure love for regexes, but we would have appreciated if you tried yourself first.

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