Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^5: Manipulating data structure

by Cristoforo (Deacon)
on Aug 17, 2012 at 15:14 UTC ( #987994=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Manipulating data structure
in thread Manipulating data structure

Hi SerZKO

this line: if ($key =~ /^(.*)(\d+)$/) should be if ($key =~ /^(.*?)(\d+)$/)

The reason you need the '?' there is that perl regexes are greedy. In your code (.*) consumes every character in $key and then fails to match any digits for the second group, (\d+). Then the regular expression engine will back up 1 character to try to make the match succeed. And it will, capturing the last char which is a digit. But what if your digits are more than 1 char? Your code would fail to capture all the digits since the regex was satisfied with only one of them.

The question mark makes the regex look to see if the next char is a digit, and if so, start capturing in the second group.

Chris


Comment on Re^5: Manipulating data structure
Select or Download Code
Re^6: Manipulating data structure
by SerZKO (Beadle) on Aug 17, 2012 at 18:40 UTC
    Hej Chris,

    Thanks for pointing this out !

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-10-23 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls