How do you match multiple Regex patterns for a single line of text in Java?

后端 未结 5 1613
小鲜肉
小鲜肉 2020-12-17 09:05

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

5条回答
  •  暖寄归人
    2020-12-17 09:29

    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\.............))')
    

提交回复
热议问题