Home: Perl Programming Help: Regular Expressions:
Brand new to regexp question



bktanner
Novice

Jul 18, 2009, 1:15 PM


Views: 15518
Brand new to regexp question

I have never used regular expressions before, but I was wondering how I would search for a string of the form:

[#.#.#.#] or in other words [number.number.number.number]

I am parsing a file and need to extract all of those strings of that form


bktanner
Novice

Jul 18, 2009, 1:40 PM


Views: 15512
Re: [bktanner] Brand new to regexp question

Here is the regexp I have:

if ($string = /\[([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\]/) {
print $string;

Clearly this isn't right =D


shawnhcorey
Enthusiast


Jul 18, 2009, 3:27 PM


Views: 15503
Re: [bktanner] Brand new to regexp question


In Reply To
I have never used regular expressions before, but I was wondering how I would search for a string of the form:

[#.#.#.#] or in other words [number.number.number.number]

I am parsing a file and need to extract all of those strings of that form



Code
if( my @captured = $_ =~ m{ \A \[ (\d+) \. (\d+) \. (\d+) \. (\d+) \] \z }msx ){ 
print "The numbers are: @captured\n";
}else{
warn "unrecognized format for $_\n";
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


bktanner
Novice

Jul 18, 2009, 3:46 PM


Views: 15501
Re: [shawnhcorey] Brand new to regexp question

Ah alright cool, I will try it out.

For my benefit (if you have the time that is) would you mind explaining what exactly is going on? In other words, break down the expression and such for me =)?


bktanner
Novice

Jul 18, 2009, 3:51 PM


Views: 15499
Re: [bktanner] Brand new to regexp question

Also something else I am not quite clear on, and maybe this is a stupid question, but will this check every string in the file one line at a time and be able to locate a string in the form [#.#.#.#] Regardless of what else is on that line?

I suppose what I am asking is if the file I am reading is something like:

1 blahblahblahblah [#.#.#.#] blahblah
2 blahblahetc.etc.etc.etc.
3 blahblahblahblah [#.#.#.#] etc.

Will it be able to extract that string from each line, or does the file have to be of the form:

1 [#.#.#.#]
2 [#.#.#.#]
3 [#.#.#.#]

I guess essentially what I am asking is, does this check every string on a line regardless of what all is on that line to check and see if the string is of that form, or will this only check one string per line?


shawnhcorey
Enthusiast


Jul 18, 2009, 4:28 PM


Views: 15497
Re: [bktanner] Brand new to regexp question


In Reply To
Also something else I am not quite clear on, and maybe this is a stupid question, but will this check every string in the file one line at a time and be able to locate a string in the form [#.#.#.#] Regardless of what else is on that line?

I suppose what I am asking is if the file I am reading is something like:

1 blahblahblahblah [#.#.#.#] blahblah
2 blahblahetc.etc.etc.etc.
3 blahblahblahblah [#.#.#.#] etc.

Will it be able to extract that string from each line, or does the file have to be of the form:

1 [#.#.#.#]
2 [#.#.#.#]
3 [#.#.#.#]

I guess essentially what I am asking is, does this check every string on a line regardless of what all is on that line to check and see if the string is of that form, or will this only check one string per line?


No, the code assumes only one [#.#.#.#] is on the line. This will extract multiple instances.

Code
#!/usr/bin/perl 

use strict;
use warnings;

while( <DATA> ){
if( my @captured = $_ =~ m{ \[ (\d+) \. (\d+) \. (\d+) \. (\d+) \] }cgmsx ){
print "found @captured\n";
}
}
__DATA__
[0.0.0.0]
this is a test [232.34.03489.238327983]
[1.2.3.4] [1.2.3.4] [1.2.3.4] [1.2.3.4] [1.2.3.4]


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


ichi
User

Jul 19, 2009, 5:55 PM


Views: 15479
Re: [bktanner] Brand new to regexp question


Code
while(<>){ 
chomp;
if ( index($_ , "[") != -1 ){
@s = split /\]/;
@s1 = split /\[/,$s[0];
if ( $s1[-1] =~ /^[0-9.]+$/){
print $s1[-1] ."\n";
}
}
}

NB: not for multiple [..]s in line.


(This post was edited by ichi on Jul 19, 2009, 5:57 PM)