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

Here's an example string:
confederation-as-router: 1 3277 myrouter1-cr2
I can match everything that I DO want, just not what I DONT want. I've got:
$_ =~ s/^confederation-as-router\:\W+\d\W+\d+\W//
This would return:
dsdfge1-cr4
in the above example. I want the regex to match any entries NOT ending in cr(digit).

Replies are listed 'Best First'.
Re: How do you match
by chromatic (Archbishop) on Sep 29, 2000 at 00:27 UTC
    You could use the !~ operator. I like unless for this, though.
    unless (/cr\d$/) { print "This entry didn't match!\n"; }
    The other could be:
    if ($_ !~ /cr\d$/) { print "Didn't match.\n"; }
      How would I append this rule to my existing regex?
        You can't "append" it per se. You can't just say you want part of a regexp to match and another part to fail for the thing to succeed. Regular expressions are meant to match something, not "not" match. There's a negative lookahead thing (?!pattern) but that wasn't meant for what you're trying to do. You need to design your regular expression to succeed, even if you intend to negate its results when you're done (like with unless or !~).

        Perhaps if you give us a few lines of input and tell us what is supposed to match, and if so, what part of that string you expect your regexp to pull out, and we can try and point you in the right direction (or even write it).

RE: How do you match
by runrig (Abbot) on Sep 29, 2000 at 00:58 UTC
    Use a negative lookahead assertion (and you don't need the '$_ =~' when its the '$_' variable):
    s/^confederation-as-router\:\W+\d\W+\d+\W(?!.*cr\d$)//;
RE: How do you match
by geektron (Curate) on Sep 29, 2000 at 00:46 UTC
    follow chromatic's advice if you simply want to match 'NOT cr{digit}'.

    but your regex already matches entries that don't end in cr{digit}. i fiddled with the string a bit, and your regex returns whatever the last field is (like 'myrouter1-cr2').

    so if you're parsing a text file and looking for instances of 'confederation-as-router' and want to return all the aliases ( or whatever the last field is ) you're already there.

    otherwise, a bit more info might help all concerned to help 'append to the regex'