----- # Custom isHidden: false menupriority: 1 kind: article created_at: 2010-02-15T11:16:12+02:00 title: All but something regexp multiTitle: fr: Expression Régulière pour "Tout sauf un mot" en: Pragmatic Regular Expression Exclude multiDescription: fr: pas de description. en: Sometimes you want to exclude some string with an existing regular expression. Here are some examples on how you could achieved that when it is possible (it is not always the case). tags: - regexp ----- Sometimes you cannot simply write:
if str.match(regexp) and
not str.match(other_regexp)
do_something
# match all string containing 'bull' (bullshit comprised)
/bull/
# match all string containing 'bull' except 'bullshit'
/bull([^s]|$)|
bulls([^h]|$)|
bullsh([^i]|$)|
bullshi([^t]|$)/
# another way to write it would be
/bull([^s]|$|s([^h]|$)|sh([^i]|$)|shi([^t]|$))/
# Begin with 'a': ^a
# End with 'a': c$
# Contain 'b': .*b.*
# But isn't 'axbxc'
if str.match(/^a.*b.*c$/) and
not str.match(/^axbxc$/)
do_something
end
/abc| # length 3
a.bc| # length 4
ab.c|
a[^x]b[^x]c| # length 5
a...*b.*c| # length >5
a.*b...*c/