in reply to Re: How do you match
in thread How do you match

How would I append this rule to my existing regex?

Replies are listed 'Best First'.
RE: RE: Re: How do you match
by Fastolfe (Vicar) on Sep 29, 2000 at 00:46 UTC
    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).

      Ok, here's (hopefully) a more complete example: Example strings to match:
      confederation-as-router: 1 2345 atlanta1-cr1 confederation-as-router: 1 2345 atlanta1-cr2 confederation-as-router: 1 2345 atlanta1-cr3 confederation-as-router: 1 2345 atlanta1-cr4 confederation-as-router: 1 2345 atlanta1-cr5 confederation-as-router: 1 2345 atlanta1-cr6 confederation-as-router: 1 2345 atlanta1-sr1
      open (FILE2, $file_2) or die "Couldn't open file: $!"; while (<FILE2>) { if ($_ =~ s/^confederation-as-router\:\W+\d\W+\d+\W//) { push(@file2, $_); } } </CODE> Currently, the regex will return:
      atlanta1-cr1 atlanta1-cr2 atlanta1-cr3 atlanta1-cr4 atlanta1-cr5 atlanta1-cr6 atlanta1-sr1
      Based upon the above example, I want it to ONLY return:
      atlanta1-sr1
      Is that clearer?
        It might be simplest to do this:
        print if !/cr\d$/ && $_ = (split)[3];
        You're basically breaking apart by spaces anyway, and if you can assume that each line will follow this pattern, it might be simplest to use split and only use a regular expression to be sure you're working with the right line. Even if you do use a regular expression like yours, it's probably easiest to break it apart anyway:
        if (!/cr\d$/ && s/^confederation-as-router:\W+\d\W+\d\W//) { ... }
        or even
        if (!/cr\d$/ && ($host, $last) = (split)[0,3]) { print $last if $host eq 'confederation-as-router:'; }

        Don't do that with a single regex.

        while( <FILE> ) { if( s/^confederation-as-router\:\W+\d\W+\d+\W// && ! /cr\d$/ ) { push(@file2, $_); } }

                - tye (but my friends call me "Tye")