How to combine multiple regex into single one in python?

前端 未结 3 805
长发绾君心
长发绾君心 2020-11-30 10:18

I\'m learning about regular expression. I don\'t know how to combine different regular expression to make a single generic regular expression.

I want to write a sing

3条回答
  •  心在旅途
    2020-11-30 10:54

    You need to compile all your regex functions. Check this example:

    import re
    re1 = r'\d+\.\d*[L][-]\d*\s[A-Z]*[/]\d*'
    re2 = '\d*[/]\d*[A-Z]*\d*\s[A-Z]*\d*[A-Z]*'
    re3 = '[A-Z]*\d+[/]\d+[A-Z]\d+'
    re4 = '\d+[/]\d+[A-Z]*\d+\s\d+[A-z]\s[A-Z]*'
    
    sentences = [string1, string2, string3, string4]
    for sentence in sentences:
        generic_re = re.compile("(%s|%s|%s|%s)" % (re1, re2, re3, re4)).findall(sentence)
    

提交回复
热议问题