Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Need advice on PERL

by kabeldag (Hermit)
on Dec 12, 2006 at 08:59 UTC ( [id://589235]=note: print w/replies, xml ) Need Help??


in reply to Need advice on PERL

- ^ is match at beginning ..
- \d is looking for a digit character
- + is find \d 1 or more times
- (...) Groups subexpressions for capturing to $1, $2,$3 ...

In this case, $scrip will return the the complete sub expression match (which is inside the brackets : (\d+\.\d+\.\d+\.\d+) ):
my ($scrip) = $ARGV[0] =~ /^(\d+\.\d+\.\d+\.\d+)/; print "\$scrip returned : $scrip\n"; print "match 1 : $1\nmatch 2 : $2\nmatch 3 : $3\nmatch 4 : $4\n";
INPUT
C:\Perl\bin>perl bleh.pl 1.0.3.3_1.3.45.44

OUTPUT
$scrip returned : 1.0.3.3
match 1 : 1.0.3.3
match 2 :
match 3 :
match 4 :

If "\d+\.\d+\.\d+\.\d+" wasn't inside the brackets, $scrip would return 1, if it matched the reg-ex:
my ($scrip) = $ARGV[0] =~ /\d+\.\d+\.\d+\.\d+/; if($scrip) { print "matched $ARGV[0] !\n"; }else{ print "did not match $ARGV[0] !\n"; } print "\$scrip = $scrip\n";
OUTPUT EXAMPLES:

C:\Perl\bin>perl bleh.pl 13.3.3.3_
matched 13.3.3.3_ !
$scrip = 1

C:\Perl\bin>perl bleh.pl 13.3.
did not match 13.3. !
$scrip =

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://589235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-25 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found