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!!!:::