Combining two regex expressions into one
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]+)
精彩评论