Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: "everything but" regex

by HyperZonk (Friar)
on Aug 16, 2001 at 23:31 UTC ( #105482=note: print w/replies, xml ) Need Help??

in reply to "everything but" regex

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'.


Replies are listed 'Best First'.
(elbie): "everything but" regex
by elbie (Curate) on Aug 17, 2001 at 01:15 UTC
    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?


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://105482]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2023-06-06 09:38 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (26 votes). Check out past polls.