Merge several regular expressions into one RE

前端 未结 5 2376
暖寄归人
暖寄归人 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:41

    Using | in your regex and re.findall() is probably the way to go, here is an example:

    >>> pattern = re.compile(r"(\d{1,3}[a-zA-Z]?/\d{1,3}[a-zA-Z]?|\babc\b)")
    >>> pattern.findall("*some string* 100/64h *some string* 120h/90 *some string* abc 200/100 abc *some string* 100h/100f")
    ['100/64h', '120h/90', 'abc', '200/100', 'abc', '100h/100f']
    

    If it is valid for your patterns to overlap then this won't work.

提交回复
热议问题