Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: extracting number from string

by puudeli (Pilgrim)
on Mar 05, 2009 at 10:39 UTC ( [id://748480]=note: print w/replies, xml ) Need Help??


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

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};

Replies are listed 'Best First'.
Re^4: extracting number from string
by samuelalfred (Sexton) on Mar 05, 2009 at 12:54 UTC
    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!

        And further, the regex metacharacter  ^ at the beginning of a character set complements the characters specified in the set, so that  [^\d.,] means "any character except a decimal digit, full-stop or comma".

Log In?
Username:
Password:

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

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

    No recent polls found