Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2016-10-22 16:56 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (297 votes). Check out past polls.