php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

后端 未结 6 2105
清酒与你
清酒与你 2020-12-05 21:26

how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01) ? I have a regex but doesn\'t seem to play well with commas

preg_match(\'/([0-         


        
6条回答
  •  醉话见心
    2020-12-05 21:43

    You can use this regex: -

    /((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/
    

    Explanation: -

    /(
        (?:[0-9]+,)*   # Match 1 or more repetition of digit followed by a `comma`. 
                       # Zero or more repetition of the above pattern.
        [0-9]+         # Match one or more digits before `.`
        (?:            # A non-capturing group
            \.         # A dot
            [0-9]+     # Digits after `.`
        )?             # Make the fractional part optional.
     )/
    

提交回复
热议问题