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

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?

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

    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")
      Why not?
      while( <FILE> ) { push @file2, $1 if m/^confederation-as-router:\W+\d\W+\d+\W(?!.*cr\d ++)$/; }
      Although I bet you could improve on that further... reducing the .* to something more specific.

      Update: I missed the negation in that. oops. I've added it to my regex. Thanks runrig and Tye for pointing that out to me. (For those who missed it, I forgot the ?!)