Regex to remove all (non numeric OR period)

后端 未结 5 878
陌清茗
陌清茗 2021-02-02 04:46

I need for text like \"joe ($3,004.50)\" to be filtered down to 3004.50 but am terrible at regex and can\'t find a suitable solution. So only numbers and periods should stay -

5条回答
  •  北海茫月
    2021-02-02 05:26

    The approach of removing offending characters is potentially problematic. What if there's another . in the string somewhere? It won't be removed, though it should!

    Removing non-digits or periods, the string joe.smith ($3,004.50) would transform into the unparseable .3004.50.

    Imho, it is better to match a specific pattern, and extract it using a group. Something simple would be to find all contiguous commas, digits, and periods with regexp:

    [\d,\.]+
    

    Sample test run:

    Pattern understood as:
    [\d,\.]+
    Enter string to check if matches pattern
    >  a2.3 fjdfadfj34  34j3424  2,300 adsfa    
    Group 0 match: "2.3"
    Group 0 match: "34"
    Group 0 match: "34"
    Group 0 match: "3424"
    Group 0 match: "2,300"
    

    Then for each match, remove all commas and send that to the parser. To handle case of something like 12.323.344, you could do another check to see that a matching substring has at most one ..

提交回复
热议问题