in reply to Referencing a Backreference

Well, there is actually more that I'm matching on. This is just a portion of it. Here is a little more background if you are interested Splitting Across Multiple Lines. I tried for two days trying to split on this text, but could not come up with a good solution. Regex work better for me in this case.

I appreciate the suggestions, but I'm still wondering if there is a way to reference the regex like I'm trying to do.

Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Re: Referencing a Backreference
by thunders (Priest) on Mar 14, 2002 at 18:59 UTC
    put the ip in a variable.
    Update:The error was actually in the original regex given not properly matching the names, as far as I can tell, but i think it's much more readable to use a variable name instead of $1, which I think looks confusing and non-intuitive inside of a pattern match.
    #!/usr/bin/perl -w use strict; my @addrs = ( "192.168.89.1 acmeorp.acme.com", "192.168.31.3 ftp.acme.com", "192.168.19.179 [Unknown]" ); my (@ips,@names); for (@addrs) { push (@ips, $1) if /\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/; my $ip = $1; push (@names, $1) if /\Q$ip\E\s+([\w.]+[^\s]*$|\[Unknown\])\s*?/; } print @names;