Home > Archive > PERL Beginners > June 2007 > regular expressions issue
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
regular expressions issue
|
|
| Amichai Teumim 2007-06-27, 9:59 pm |
| I created a file called data.txt which contains a bunch of junk, including
some IPs. I want $line to be stored in
$ip<http://www.tek-tips.com/viewthread....1382614&page=1#>
..
It works, except for the regular expressions which should find only IPs. If
I use the regular expression with the grep command in terminal I get only
the IPs. Here in Perl I don't get any output.
#!/usr/bin/perl
@input = `cat ~/ip.txt`;
foreach $line (@input){
if($line =~
/[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/){
$ip = $line;
print $ip;
}
}
Any ideas? It's breaking my head.
Amichai
| |
| Tom Phoenix 2007-06-28, 3:59 am |
| On 6/27/07, Amichai Teumim <amichai@teumim.com> wrote:
> If I use the regular expression with the grep command in
> terminal I get only the IPs. Here in Perl I don't get any output.
The grep command uses grep's regular expressions, but Perl uses Perl's
regular expressions. Alas, everybody's regular expressions are
different. Perl's are usually better, of course. But the syntax is
always different.
> @input = `cat ~/ip.txt`;
I hope that this is _supposed_ to be a quick-and-dirty program. This
works, although it's slower than using a filehandle would be, and it
probably uses more memory. Although if you're using the tilde to open
a file in the user's home directory, well, that's maybe the best way
to do it.
> /[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/){
I think in Perl that pattern might be this:
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
But do you really want to match 999.999.999.999? You don't have to.
Have you heard of Regexp::Common? Regexp::Common::net seems to have
what you want.
/^$RE{net}{IPv4}$/
http://search.cpan.org/~abigail/Reg...egexp/Common.pm
http://search.cpan.org/dist/Regexp-...p/Common/net.pm
Even if you don't want to install the module to get just one pattern,
you could use the pattern that it supplies, which is sure to be at
least as good as anything you would write on your own.
Good luck with it!
--Tom Phoenix
Stonehenge Perl Training
| |
| Rob Dixon 2007-06-28, 3:59 am |
| Amichai Teumim wrote:
>
> I created a file called data.txt which contains a bunch of junk, including
> some IPs. I want $line to be stored in
> $ip<http://www.tek-tips.com/viewthread....1382614&page=1#>
> .
>
> It works, except for the regular expressions which should find only IPs. If
> I use the regular expression with the grep command in terminal I get only
> the IPs. Here in Perl I don't get any output.
>
> #!/usr/bin/perl
>
> @input = `cat ~/ip.txt`;
>
> foreach $line (@input){
> if($line =~ /[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/){
>
> $ip = $line;
> print $ip;
> }
> }
>
> Any ideas? It's breaking my head.
Perl doesn't require the braces to be escaped. As it is the regex is matching literal
braces in the string which don't exist. Try this:
if ($line =~ /[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/) {
:
}
and, by the way, [0-9] is more concise than [[:digit:]].
HTH,
Rob
|
|
|
|
|