CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



bktanner
Novice

Jul 18, 2009, 1:15 PM

Post #1 of 7 (7329 views)
Brand new to regexp question Can't Post

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

Post #2 of 7 (7323 views)
Re: [bktanner] Brand new to regexp question [In reply to] Can't Post

Here is the regexp I have:

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

Clearly this isn't right =D


shawnhcorey
Enthusiast


Jul 18, 2009, 3:27 PM

Post #3 of 7 (7314 views)
Re: [bktanner] Brand new to regexp question [In reply to] Can't Post


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

Post #4 of 7 (7312 views)
Re: [shawnhcorey] Brand new to regexp question [In reply to] Can't Post

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

Post #5 of 7 (7310 views)
Re: [bktanner] Brand new to regexp question [In reply to] Can't Post

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

Post #6 of 7 (7308 views)
Re: [bktanner] Brand new to regexp question [In reply to] Can't Post


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

Post #7 of 7 (7290 views)
Re: [bktanner] Brand new to regexp question [In reply to] Can't Post


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)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives