Exclusive Or in Regular Expression

后端 未结 13 1766
小鲜肉
小鲜肉 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:34

    Using the word boundaries, you can get the single word...

    me@home ~  
    $ echo "Where is my bar of soap?" | egrep "\bfoo\b|\bbar\b"  
    Where is my bar of soap?  
    
    me@home ~  
    $ echo "What the foo happened here?" | egrep "\bfoo\b|\bbar\b"  
    What the foo happened here?  
    
    me@home ~  
    $ echo "Boy, that sure is foobar\!" | egrep "\bfoo\b|\bbar\b"  
    

提交回复
热议问题