Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: How can i extract a number from an string

by johngg (Canon)
on Jan 22, 2009 at 14:57 UTC ( [id://738179]=note: print w/replies, xml ) Need Help??


in reply to Re: How can i extract a number from an string
in thread How can i extract a number from an string

An alternative to JavaFan's use of a capture in the split regexp could be to use look-arounds to do the split at points where the string changes from non-digit to digit and vice-versa. I think JavaFan's method is better in this case but the look-arounds can sometimes be a useful technique.

$ perl -le ' > $_ = q{BS123-bleble}; > print +( split m{(?x) (?: (?<=\D)(?=\d) | (?<=\d)(?=\D) )} )[ 1 ];' 123 $

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: How can i extract a number from an string
by Sombrerero_loco (Beadle) on Jan 23, 2009 at 11:02 UTC
    Wow....such complicate to extract the number. I never think its takes too many code to extract just a number, i thought using split the code must be easier. I'll try it today and keep you posted.. Thanks everyone for the replies!
    :::When you dream, there're no rules, ppl can fly...anything can happen!!!:::

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found