Filter string REGEX C#

Asked

Viewed 282 times

4

I have a string that returns all the content of an html page.

On this page, you have the following line:

<input style="width: 2.3em;" id="nacional" value="3,48" type="text">

I need only the value that is in value, that is to say, 3,48, remembering that this value changes automatically every day.

How can I filter this line and get the value?

2 answers

3

I made a Regexr with your pattern. The regular expression is:

value=\"([\d,]+)\"

In C# it looks a little different.

var regex = new Regex(@"value=""([\d,]+)""");

Once done, just select group 1 (group 0 is the entire match found). That is to say:

var match = regex.Match("<input style=\"width: 2.3em;\" id=\"nacional\" value=\"3,48\" type=\"text\">");
Console.WriteLine(match.Groups[1].Value);

I made a Fiddle.

1

Follow an example that returns any character between double quotes or apostrophe, within the value attribute.

value=['"](.*?)['"]
  • and how could I implement on this line? Matchcollection M1 = Regex.Matches()

  • This is just the Pattern, any doubt worth taking a look over Regex C#

Browser other questions tagged

You are not signed in. Login or sign up in order to post.