1

I am looking for a regular expression to find all input fields of type hidden in html output. Anyone know an expression to do such?

H H
  • 263,252
  • 30
  • 330
  • 514
amateur
  • 43,371
  • 65
  • 192
  • 320

3 Answers3

2

I agree that the link Radomir suggest is correct that HTML should not be parsed with regular expressions. However, I do not agree that nothing meaningful can be gleaned from their use together. And the ensuing rant is totally counter-productive.

To correct Robert's RegEx:

<([^<]*)type=('|")hidden('|")>[^<]*(/>|</.+?>)

Brad
  • 15,361
  • 6
  • 36
  • 57
  • Not even close. For example, try `` or ``. And both of those examples are valid html. Never mind the problems when processing real world html. Use a parser. – Alohci Sep 16 '10 at 19:04
  • @Alohci, *no doubt* you should use a parser if you can for ANYTHING xml. @Niall, if you need the optional spaces in the expression to handle the cases Alohci brought up, it shouldn't be too hard. Ugly, yes, but not too hard. – Brad Sep 16 '10 at 20:54
2

I know you asked for regular expression, but download Html Agility Pack and do the following:

var inputs = htmlDoc.DocumentNode.Descendants("input");
foreach (var input in inputs)
{
   if( input.Attributes["type"].Value == "hidden" )
   // do something
}

You can also use xpath with html agility pack.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
0

Regular expressions are generally the wrong tool for the job when trying to search or manipulate HTML or XML; a parsing library would likely be a much cleaner and easier solution.

That said, if you're just looking through a big file and accuracy isn't critical, you can probably do reasonably well with something like <input[^>]*type="?hidden"?.

ngroot
  • 1,186
  • 5
  • 11
  • ngroot, that expression is only a partial match. – Brad Sep 16 '10 at 17:44
  • That's correct. He asked for an expression that would find these tags, which this will usually do. What's it matter if it matches on the whole tag? – ngroot Sep 16 '10 at 17:49
  • I don't think finding *half* of the tag will really help, but I see your point. It won't let me un-down-vote you though. – Brad Sep 16 '10 at 20:55
  • Half the tag is just fine if you are, as the author requested, just looking to *find* the tags. That's what I usually do if I'm doing ad-hoc searches of documents; I use the shortest expression that will take me to what I'm looking for. If he wants to do something more complex, like replace them, a regex is really not a safe tool to be using anyway. – ngroot Sep 16 '10 at 22:51