Re: Negate a regular expression



Chad Perrin wrote:
On Fri, Dec 29, 2006 at 11:36:25PM -0500, Mathew Snyder wrote:
I'm trying to set up an if clause to exit if an answer given is anything but
either any case combination of 'y' or 'yes'. This is what I have:

exit if $ans =~ m/[^y|^yes]/i;

Will that do what I want?

If you want anything but a given pattern, the best way to do it is
usually by using the !~ operator. Thus, if you have a pattern $foo,
this matches that pattern:

exit if $ans =~ m/$foo/i;

. . . while this matches anything but that pattern:

exit if $ans !~ m/$foo/i;


Thanks. That looks like exactly what I was looking for.

Mathew
.



Relevant Pages

  • Re: Calculate Last Digits
    ... This might work faster by adding the following line at this point: ... Exit For, as discussed at enormous length in older threads, but this is ... I did get a big boost by switching from strings to longs for the pattern ...
    (microsoft.public.vb.general.discussion)
  • Re: regexp (vim6)
    ... pattern would match Function, unless preceeded by Exit or End - which is ... Exit or End, followed by Function. ... Prev by Date: ...
    (comp.editors)
  • what are the USERID,PASSWD in /etc/passwd
    ... echo "Usage: `basename $0` USERNAME" ... exit $E_WRONGARGS ... file_excerpt $pattern ...
    (comp.unix.shell)
  • Re: Print this line & next
    ... > want to test for getline succeeding before printing, ... There's no exit() because the pattern may appear in the input ... as context can be specified as command line option (the default is ...
    (comp.unix.shell)
  • Re: A question about subprocess
    ... will need to be run in the following pattern: ... when the remote job is done. ... Clean exit might have been a better word. ... ssh to exit before the job is done; other things can make the process ...
    (comp.lang.python)