Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

"everything but" regex

by melguin (Pilgrim)
on Aug 16, 2001 at 23:23 UTC ( [id://105478]=perlquestion: print w/replies, xml ) Need Help??

melguin has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write an expression that matches everything but a specific string. Yes, I know that I can do it other ways outside of the regex, but I'd like to know how to do it inside, for learning if for no other reason (do we need another reason?).

I know that if I don't want any upercase letters I can do:

/[^A-Z]/
So I would assume I could also do a string that way if I blocked it off like this:
/[^(mystring)]/
Anyone see any problems with this, or perhaps a better expression?

melguin.

Replies are listed 'Best First'.
Re: "everything but" regex
by HyperZonk (Friar) on Aug 16, 2001 at 23:31 UTC
    It depends on what you mean by "match." If you want the regex to be true for every string except the regex, you can use the "not bind" operator (my name for it, not the official one) as such:
    $string !~ /whatever/
    On the other hand, if you want to capture everything that doesn't match, you would have to do something along the lines of
    $string =~ /(.*)whatever(.*)/;
    with the matches around "whatever" being $1 and $2.

    Update: As noted by Albannach, if you are looking for exact matches only, you should use /^whatever$/ instead in the first example. The original version will also match 'This whatever that.' whereas the updated version will only match 'whatever'.

    -HZ
      Note that if your string does not contain 'whatever', then HyperZonk's second regex will return nothing.

      I'm going to go out on a limb here, as regexes are not my thing, but this might just work:

      $string =~ /^((.*?)(whatever))*(.*)$/;

      However to be perfectly honest, I've no idea which $1, $2, $3, etc will be the values you want, or even if the regex is correctc.

      I seem to have bitten off a bit much. Can somebody help?

      elbieelbieelbie

Re: "everything but" regex
by chipmunk (Parson) on Aug 16, 2001 at 23:38 UTC
    A character class is just that, a character class. A character class will either match a single character, or it won't. It cannot be used for negative matching of multi-character strings. The character class [^(mystring)] will match any character that is not (, m, y, s, t, r, i, n, g, or ).

    You can get the effect you want with a negative look-ahead assertion: /^(?!mystring\z)/; That's basically equivalent to (but much less readable than): $_ ne 'mystring';

Re: "everything but" regex
by dga (Hermit) on Aug 16, 2001 at 23:34 UTC

    the brackets match character classes i.e. a one of match you want something like

    !/mystring/

    This will reject any input string with the text 'mystring' in it.

    Yours would find any text which had no (,),m,y,s,t,r,i,n,g characters in it at all.

Re: "everything but" regex
by japhy (Canon) on Aug 17, 2001 at 07:44 UTC
    It depends what you want. If you just want to make sure that the string can't match a regex, you can use the !~ operator, or you can make a regex like:
    if ($string =~ /\A(?:(?!regex).)*\z/s) { # doesn't contain regex anywhere }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105478]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-19 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found