简体   繁体   中英

PHP RegEx match string with random number at end

Currently learning PHP with RegEx and I want to extract the DAX value from a website.
Got the website source with file_get_contents and then cleaned up the tags with strip_tags .

The block that contains the string i want is this one: Dax5.502-2,4%

but i just need the value 5.502

The regex code I have so far is '/@Dax\\d.\\d\\d\\d[+-]\\d,\\d[%]$/'

But it doesnt work. can anyone tell me how to do this correctly.

Thanks :)

"/Dax(.*)-/"

您的结果将是数字

/^Dax([0-9].[0-9]{3})/

有了这个(您所需要的),您的结果就是数字。

You do not need to use a regular expression to parse the formatted string, sscanf Docs looks like a more fitting way to parse the float value ( Demo ):

$str = 'Dax5.502-2,4%';

sscanf($str, 'Dax%f', $value);

echo $value; # 5.502

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM