Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: regex not working

by jwkrahn (Monsignor)
on Oct 07, 2011 at 07:54 UTC ( #930140=note: print w/ replies, xml ) Need Help??


in reply to regex not working

Well let's see what your program does:

$ perl -le' use warnings; use strict; print "Enter number : "; chomp( my $num = <STDIN> ); print "\$num = $num"; my @nums = split /(?<=[\d])/, $num; print "\@nums = @nums"; my $i = 0; for ( @nums ) { my $regex = qr/[$i]{$_}/; print "\$regex = $regex"; print $num =~ $regex ? "true" : "false"; $i++; } ' Enter number : 2020 $num = 2020 @nums = 2 0 2 0 $regex = (?-xism:[0]{2}) false $regex = (?-xism:[1]{0}) true $regex = (?-xism:[2]{2}) false $regex = (?-xism:[3]{0}) true

On the first pass you are trying to match '2020' with /[0]{2}/, or in other words with /00/.    Since '2020' does not contain the string '00' the program prints false.

On the second pass you are trying to match '2020' with /[1]{0}/, or in other words with //.    Since '2020' contains five places that will match the program prints true.

$ perl -le'$x = () = "2020" =~ /[1]{0}/g; print $x' 5

The next two follow the same pattern as the first two.


Comment on Re: regex not working
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2015-07-30 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls