View Full Version : How to negate a POSIX expression?
Paddy Landau
August 22nd, 2012, 02:54 PM
Suppose I want a POSIX ERE (Extended Regular Expression) to find all strings beginning with "gout". That's easy:
^goutBut how do I negate this, to find all strings not starting with "gout"? I have been Googling for ages and I cannot find the answer.
cortman
August 22nd, 2012, 02:56 PM
I forget if this is POSIX compliant or not but
^[^gout]
Would do the trick.
EDIT: It is (http://www.boost.org/doc/libs/1_44_0/libs/regex/doc/html/boost_regex/syntax/basic_extended.html).
Paddy Landau
August 22nd, 2012, 03:05 PM
I forget if this is POSIX compliant or not but
^[^gout]Would do the trick.
No… That says that the first character may not be any of g, o, u or t. Thus, "bout" would be (correctly) allowed whereas "grab" would be (incorrectly) rejected.
diesch
August 22nd, 2012, 03:14 PM
There is no simple way to do this with POSIX ERE. One way would be
^([^g]|g[^o]|go[^u]|gou[^t])
ofnuts
August 22nd, 2012, 03:52 PM
There is no simple way to do this with POSIX ERE. One way would be
^([^g]|g[^o]|go[^u]|gou[^t])
This explains why grep has a -v option, and perl a "!~" operator :)
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.