How to concisely cascade through multiple regex statements in Python

前端 未结 7 845
天涯浪人
天涯浪人 2020-12-08 05:51

My dilemma: I\'m passing my function a string that I need to then perform numerous regex manipulations on. The logic is if there\'s a match in the first regex, do one thing

7条回答
  •  执笔经年
    2020-12-08 06:06

    Are the manipulations for each regex similar? If so, try this:

    for regex in ('regex1', 'regex2', 'regex3', 'regex4'):
        match = re.match(regex, string)
        if match:
            # Manipulate match.group(n)
            return result
    

提交回复
热议问题