How to concisely cascade through multiple regex statements in Python

前端 未结 7 844
天涯浪人
天涯浪人 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 05:59

    Hmm... you could use something with the with construct... um

    class rewrapper()
        def __init__(self, pattern, target):
            something
    
        def __enter__(self):
            something
    
        def __exit__(self):
            something
    
    
     with rewrapper("regex1", string) as match:
        etc
    
     with rewrapper("regex2", string) as match: 
        and so forth
    

提交回复
热议问题