user10311801
user10311801

Reputation:

Get html input controls and their values from html string

I got string str = "<input type=\"hidden\" id=\"taken\" value=\"BoboBobo\">\n<input type=\"hidden\" id=\"took\" value=\"BaboboBe\""

How do I get values from these input controls from html string using C#?

Upvotes: 1

Views: 860

Answers (3)

Stefan Illner
Stefan Illner

Reputation: 179

You can use this code to iterate over all <input>-values using a regular expression:

var matches = Regex.Matches(str, "<input\\b.*?\\s+value\\s*=\\s*\"([^\"]*)\"");
foreach (var match in matches)
  Console.WriteLine(((Match)match).Groups[1].Value);

This prints the values one by one to the console.

Maybe it's a good idea to create a generic dictionary for mapping the input ids to the values.

Upvotes: 0

Poul Bak
Poul Bak

Reputation: 10929

You can use the following regex to achive your goal:

@"(?<=\svalue\s?=\s?\\"").*?(?=\\"")"

It is in an @-quoted string, so quotes must be doubled.

It starts by creating a look behind for: 'value=\"', then it matches any characters not being a backslash (zero or more times, non greedy), and finally looks forward for a backslash and a double quote.

How to use:

string text = "<input type=\"hidden\" id=\"taken\" value=\"BoboBobo\">\n<input type=\"hidden\" id=\"took\" value=\"BaboboBe\"";
Regex regex = new Regex(@"(?<=\svalue\s?=\s?\\"").*?(?=\\"")");
foreach (Match match in regex.Matches(text))
{
    Console.WriteLine(match.Value);
}

Edit according to comment, now it should Work.

Upvotes: 0

Lucas Anschau
Lucas Anschau

Reputation: 91

You can use split and foreach:

string yourinputshidden = "<input type=\"hidden\" id=\"taken\" value=\"BoboBobo\">\n<input type=\"hidden\" id=\"took\" value=\"BaboboBe\"";
string[] splitted = yourinputshidden.split(new[] { "\n<input" }, StringSplitOptions.None);
Dictionary<string, string> inputs = new Dictionary<string, string>(); // the list of inputs, by ID, Value
foreach(string item in splitted )
{
    string splittedAgain = item.split(new[] { "=\"" }, StringSplitOptions.None);
    string inputId = splittedAgain[3].replace("\"", "");
    string inputValue = splittedAgain[5].replace("\"", "");
    inputs.Add(inputId, inputValue);
}

// Continue your code

Upvotes: 1

Related Questions