Regular Expression to find hidden fields in html

陌路散爱 提交于 2021-01-29 04:13:45

问题


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?


回答1:


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('|")>[^<]*(/>|</.+?>)




回答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.




回答3:


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"?.



来源:https://stackoverflow.com/questions/3729105/regular-expression-to-find-hidden-fields-in-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!