开发者

Combining two regex expressions into one

开发者 https://www.devze.com 2023-01-11 05:17 出处:网络
I have a regex that works: ABC-[0-9]+ I also have a rege开发者_如何学编程x: DEF-[0-9]+ But I don\'t get how to combine the two so it will match them both

I have a regex that works: ABC-[0-9]+

I also have a rege开发者_如何学编程x: DEF-[0-9]+

But I don't get how to combine the two so it will match them both

I tried ABC-[0-9]+ | DEF-[0-9]+ but it didn't really work...

This is all in Java regex if it matters.


If you want a regular expression that matches sequences that start with either ABC or DEF, try this:

(ABC|DEF)-[0-9]+

But except from the two space characters around |, your regular expression should match that too:

ABC-[0-9]+|DEF-[0-9]+

These two regular expressions match the same set of strings.


You need to group the two regexes, use an atomic group:

(?>ABC-[0-9]+)|(?>DEF-[0-9]+)


Try (ABC-[0-9]+)|(DEF-[0-9]+)

0

精彩评论

暂无评论...
验证码 换一张
取 消