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. Thanks for your help! 回答1: If you just want to grab the data, you can just use a loose regex: ([\d.]+)\s+(\S+) ([\d.]+) : [\d.]+ will match a