user5204184
user5204184

Reputation: 341

Regex to match a number with decimals

I have the following regex:

var matches = Regex.Matches(s, @"ratingValue"" content=""(?<content>(?:(.|\n)(?!""></span>))+)");

However, it gives the output with "number.", anything after the . is not taken. For example, if the number is 4.0 it will output 4. or if it's 3.5 it gives 3..

How can I get the decimals as well?

Example input:

<span itemprop="ratingValue" content="4.0"></span>

I need to output 4.0.

Upvotes: 0

Views: 135

Answers (2)

aegis
aegis

Reputation: 366

I've changed your regex so it returns the decimal number as the match:

var matches = Regex.Matches(s, @"(?:ratingValue"" content="")(?<content>[\r\n\d\.]*)(?:""></span>)");

Is this what you were after? It returns 4.0 from the provided sample.

Upvotes: 0

w.b
w.b

Reputation: 11228

You can use LINQ to XML:

string xml = @"<span itemprop=""ratingValue"" content=""4.0""></span>";

string content = XElement.Parse(xml).Attribute("content").Value;

decimal value = Convert.ToDecimal(content, CultureInfo.InvariantCulture);

Upvotes: 3

Related Questions