Exclusive Or in Regular Expression

后端 未结 13 1706
小鲜肉
小鲜肉 2020-12-05 10:06

Looking for a bit of regex help. I\'d like to design an expression that matches a string with \"foo\" OR \"bar\", but not both \"foo\" AND \"b

相关标签:
13条回答
  • 2020-12-05 10:53

    I don't think this can be done with a single regular expression. And boundaries may or may not work depending on what you're matching against.

    I would match against each regex separately, and do an XOR on the results.

    foo = re.search("foo", str) != None
    bar = re.search("bar", str) != None
    if foo ^ bar:
        # do someting...
    
    0 讨论(0)
提交回复
热议问题