开发者

Regex to get float from a string

开发者 https://www.devze.com 2023-01-15 21:06 出处:网络
Hay, i have a system basically tracks finances. In this application it has a \"cost\" field (which unfortunately is VARCHAR field). This field has various values entered like:

Hay, i have a system basically tracks finances. In this application it has a "cost" field (which unfortunately is VARCHAR field). This field has various values entered like:

£90
£210 per day
开发者_StackOverflow中文版£50 per logo
Design - £180
£36 p/h
£1009.51

Is there any way i can convert these to floats? I tried just using (float) to juggle the type into a float, but it isn't working.


If it's always following a £ character, you can even make sure other numbers don't match:

£\s*\d+(?:\.\d+)?

Explanation

£          # GBP
\s*        # spaces, optional
\d+        # digits, required (at least one)
(?:        # non-capturing group 
  \.\d+    #   a literal dot, and more digits
)?         # end group, make optional


/(\d+[.]?\d*)/

Edited

0

精彩评论

暂无评论...
验证码 换一张
取 消