regexstringintegerdecimal

Regex to find integers and decimals in string


I have a string like:

$str1 = "12 ounces";
$str2 = "1.5 ounces chopped;

I'd like to get the amount from the string whether it is a decimal or not (12 or 1.5), and then grab the immediately preceding measurement (ounces).

I was able to use a pretty rudimentary regex to grab the measurement, but getting the decimal/integer has been giving me problems.


Solution

  • If you just want to grab the data, you can just use a loose regex:

    ([\d.]+)\s+(\S+)
    

    You can get the number in the first capturing group, and the unit in the 2nd capturing group.

    You can be a bit stricter on the number:

    (\d+(?:\.\d*)?|\.\d+)\s+(\S+)
    

    This is not a good solution if you want to make sure you get something meaningful out of the input string. You need to define all expected units before you can write a regex that only captures valid data.