Merge several regular expressions into one RE

前端 未结 5 2474
暖寄归人
暖寄归人 2021-02-13 02:04

I have written 2 REs to match several string sequences in a String. for e.g. lets assume the two regular expressions are RE1, RE2. The strings can be i

5条回答
  •  后悔当初
    2021-02-13 02:43

    You need to escape the \ in the second RE:

    RE1 = '(\d{1,3}[a-zA-Z]?/\d{1,3}[a-zA-Z]?)'
    RE2 = '(\\babc\\b)'
    s = '*some string* 100/64h *some string* 120h/90 *some string* abc 200/100 abc *some string* 100h/100f'
    
    
    p = re.compile('('+RE2+'|'+RE1+')');
    matches = p.findall(s)
    
    for match in matches:
        print(match[0])
    

提交回复
热议问题