in reply to Re: extracting number from string
in thread extracting number from string

Thank you, but this doesn't quite do what I want. If $number = "Price 15.40", the result from

$number=~s/^\D.//g

is

$number = ice 15.40

Any idea how to get this to be just 15.40?

Thanks!

Replies are listed 'Best First'.
Re^3: extracting number from string
by puudeli (Pilgrim) on Mar 05, 2009 at 10:39 UTC

    You copied the regex incorrectly.

    perl -e '$n = "Price 15.40"; $n =~s/[^\d.]//g; print "$n\n";'
    Produces
    15.40
    --
    seek $her, $from, $everywhere if exists $true{love};
      Oh, sorry. Now its working. Could you please explain what the expression ^\d. means? Would be nice to understand how it works.

      What if I want this to work also for ","? In order words I would like it to extract all digits, "." AND ",". Is that possible?

      Thanks!

        I you enclose bits of code in <c> and </c> tags the square brackets you typed will be preserved rather than being taken as some sort of link.

        So, [\d.] denotes a character class consisting of digits and a full stop. Note that \d is in itself a character class which is the equivalent of [0-9]. Adding a comma is simplicity itself - [\d.,]. Have a look at perlretut and perlre for further information.

        I hope this will be helpful.

        Cheers,

        JohnGG

        Update: As AnomalousMonk points out, I totally forgot to explain the significance of the caret symbol at the start of a character class. Sorry!