Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
So the possibilities are that a price might fall into the following catagories:

1) 300.25
2) 300.
3) 300
4) .25

or if you want to visit Europe, replace the periods with commas.
my $price = $some_price_formatted_like_above; if ($price =~ /\d+[.,]?\d*|[.,]\d+/) {...}
...where the first portion of the pattern matches the first three possible formats listed above and the second portion matches a format like .25.
Have I missed any formats?

Cheers,
Kristina
UpdatePlease include the anchors ^$ as in Corion's update. It will prevent the if statement from running as my code would return true for 300.25.25.
Corion's code misses fractions of cents. Like if something is 99 and 1/2 cent. This may be a desirable trait though. My code misses the European spaces. All the code I've seen so far also misses things like 1,000,000,000.25. This can still be written for, but how far do you really need to go to accomplish your task?

In reply to Re: Checking for numbers and a period by krusty
in thread Checking for numbers and a period by andrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2014-09-19 21:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (147 votes), past polls