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

Re: How can i extract a number from an string

by setebos (Beadle)
on Jan 22, 2009 at 12:25 UTC ( [id://738134]=note: print w/replies, xml ) Need Help??


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

split will work only on one digit number:
(split(//,"BS0-bleble"))[2]

Replies are listed 'Best First'.
Re^2: How can i extract a number from an string
by JavaFan (Canon) on Jan 22, 2009 at 12:34 UTC
    Not if you know split:
    $ perl -wE 'say +(split/([0-9]+)/,"BS123-bleble")[1]' 123
Re^2: How can i extract a number from an string
by johngg (Canon) on Jan 22, 2009 at 14:57 UTC

    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

      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://738134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-20 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found