How Do You Match Multiple Regex Patterns for a Single Line of Text in Java?

Lets say I have multiple patterns P1, P2, P3,, and so on. These patterns are different Regex patterns to match different variations of DATE.

How do I match these for the same input text most efficiently in a piece of code.

Of course, I can write a for() to loop over these patterns one by one, but is there a better way to do this?

0

5 Answers

I think you can use the | operator of the regex and put the different regexes in paranthesis to be considered one whole regex to be matched.

("(P1)|(P2)|(P3)")

0

To complement the other answers...

You can write one big, hard-to-read pattern using the alternation operator:

r1|r2|r3|...|rn

where r1 etc are themselves "fully-fleged" regexes.

However you have to be careful about the order of alternations: the first to match wins. That is, if the regex engine is not a POSIX regex engine but java.util.regex's engine isn't.

Therefore, with text catflap, using regex:

cat|catflap

Java will match cat; a POSIX regex engine will match catflap (the longest, leftmost match).

Sticking with more individual, maintainable patterns is imho a better option.

You can use alternation (|) operator to combine multiple patterns for your regexp.

But in case you have various input and you will have to convert them to instance of Date from a string. Then you must follow in a sequence and validate the input one by one. So single regexp may validate the input but it could not be used to any other logic.

I had a similar problem. Having all the patterns in a single pattern and separating the patterns by | worked for me '((P1)|(P2)|(P3))'

My code for ref:

input_text = '''Carlisle Farm Specialist F-1 Farm Tire - 12.5L-15 LRF/12 ply (Wheel 
Not Included)       
Evergreen EU72 205/50ZR16 OWL87W RT-5 Truck tire                                        
Goodyear Marathon Trailer Tire w/Galvanized Rim ST215/75R14 LRC (5 Lug On 4.5)          
1 X New Achilles \"ATR Sport\" 265/35ZR18 97W XL High Performance Tires 265/35/18       
Set of 2 ZEEMAX Heavy Duty All Steel ST235/85R16-14PR TL Trailer Tire - 11073           
1 X New Lexani LXHT-106 LT285/70R17/8 118Q BW All Season Performance SUV Tires          
1 X New Nankang N889 Mudstar M/T LT245/75R16 120/116N E/10 Ply ROWL Mud Tires MT        
Pirelli Night Dragon Motorcycle Front Tire 130/90-16 2211500'''
#(Extract : 12.5L-15 LRF/12)
#(Extract : 205/50ZR16 OWL87W)
#(Extract : 265/35ZR18 97W XL)
#(Extract : 130/90-16 )
#(Extract : ST215/75R14 )
#(Extract : ST235/85R16-14PR TL)
#(Extract : LT285/70R17/8 118Q BW)
#(Extract : LT245/75R16 120/116N E/10)

pattern = re.compile(r'(([A-Z][A-Z]\d\d\d/\d\d\D\d\d(.[0-9]......([A-Z].([A- 
Z]...)?)?)?)|(\d\d\d/\d\d\D\D?\d\d\s\b(\w\w\w.\w[A-Z])?)|(\d\d\.............))')
0

I would not use Patterns to match dates.

You have DateFormat and SimpleDateFormat classes to do so.

This said, combining Patterns can be done with the alternation (|) operator on the String representation of the Pattern.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest