### Re: Matching numeric value between two digits

by ww (Archbishop)
 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.

Create A New User
Node Status?
node history
Node Type: note [id://1017039]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-01-23 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (254 votes). Check out past polls.

Notices?