How to combine multiple regex into single one in python?

前端 未结 3 806
长发绾君心
长发绾君心 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:34

    To findall with an arbitrary series of REs all you have to do is concatenate the list of matches which each returns:

    re_list = [
        '\d+\.\d*[L][-]\d*\s[A-Z]*[/]\d*', # re1 in question,
        ...
        '\d+[/]\d+[A-Z]*\d+\s\d+[A-z]\s[A-Z]*', # re4 in question
    ]
    
    matches = []
    for r in re_list:
       matches += re.findall( r, string)
    

    For efficiency it would be better to use a list of compiled REs.

    Alternatively you could join the element RE strings using

    generic_re = re.compile( '|'.join( re_list) )
    

提交回复
热议问题