Answer the question
In order to leave comments, you need to log in
How to extract all the attribute values of the desired tag from a line containing html?
There is a line containing html code. I want to pull out all the values of the "action" attribute of the "form" tag. I've tried many different options and haven't found a solution yet.
Split and Html Agility Pack are not suitable.
I hope there is still a solution.
UPD:
Example.
The line consists of 10 containers, which consist of DIVs, each has a FORM. From FORM, you need to extract the value of the attribute.
Answer the question
In order to leave comments, you need to log in
Why is Html Agility Pack not suitable? He just knows how to do it well.
You can, of course, write a regular expression, but the problem with HTML is that it's irregular. Those. an attribute may or may not have quotes, or it may have one. The tag may or may not be closed.
All this will have to be taken into account in the regular expression.
var matches = (new Regex("<form.*?action=(\"[^\\\"]+?\"|'[^\']+?'|[\\S]+?).*?>"
, RegexOptions.Singleline | RegexOptions.IgnoreCase)
).Matches("HTML CODE <form action=1><form action='2'><form action=\"3\">");
foreach(Match m in matches)
{
var actionValue = m.Groups[1].Value.Trim(new char[]{'\'','"'});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question