Drunkendo
Drunkendo

Reputation: 39

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 :)

Upvotes: 0

Views: 1333

Answers (3)

hakre
hakre

Reputation: 197732

You do not need to use a regular expression to parse the formatted string, sscanfDocs 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

Upvotes: 0

Mike
Mike

Reputation: 1042

"/Dax(.*)-/"

your result will be the number

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

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

With that (which is all you need), your result will be the number.

Upvotes: 1

Related Questions