Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Matching numeric value between two digits

by ww (Bishop)
on Feb 05, 2013 at 01:41 UTC ( #1017039=note: print w/ replies, xml ) Need Help??

in reply to Matching numeric value between two digits

The greedy death-star is another problem. You need to limit it:

C:\>perl -E "my $num ='abcd23.5fghi';if ($num =~ /[a-z]+(.*?)[a-z]/i){ + say $1;}" 23.5 /^\
The above is OK (Yes, I know OP wants only INTs, but this is a minimal example of the death-star problem) when the greedy ".*" is limited by the "?" but it's definitely not what the OP seems to want if the limit ("?") is omitted.
C:\>perl -E "my $num ='abcd23.5fghi';if ($num =~ /[a-z]+(.*)[a-z]/i){ +say $1;}" 23.5fgh # not what OP seems to want
Also rather than the clumsy "/\d{1}-\d{2}/" you might wish to use "/[0-9]+/" and, more verbosely this also works (but is a far less stringent regex):
C:\>perl -E "my $num ='abcd23fghi'; my $intermediate; if ($num =~ /[a- +d]+(\d+)[f-z]+/) {$intermediate = $1;} if ($intermediate =~ /[0-9]+/ +){ say $intermediate;}" 23
... whereas this fails:
C:\>perl -E "my $num ='abcd23.777fghi'; my $intermediate; if ($num =~ +/[a-d]+(\d+)[f-z]+/) {$intermediate = $1;} if ($intermediate =~ /[0-9 +]+/ ){ say $intermediate;}" (no output)

The previous discussion should make it easy to identify the cause of the failure.

Comment on Re: Matching numeric value between two digits
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-09-23 02:50 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (210 votes), past polls