locked
Regex to scrap hidden input field not in same the order RRS feed

  • Question

  • I want to scrap value of hidden input field

    <FORM onsubmit="javascript:return add();" method=post action=addMess.php>
    ....
    <INPUT name=sysess2 value=1234 type=hidden>
    ....
    </FORM>

    eg :
    <INPUT type=hidden name=hyds2 value=1234>
    <input name=hyds2 type=hidden value=1234>
    <INPUT value=1234 type=hidden name=hyds2>
    ....

    Anyone know how I can do that ?

    • Moved by Gilles TOURREAU Saturday, March 10, 2012 9:54 AM (From:Développement .NET (en général))
    Friday, March 9, 2012 8:56 PM

Answers

  • Here is a code:

    string html = "<INPUT type=hidden name=hyds2 value=1234><input name=hyds2 type=hidden value=1235><INPUT value=1236 type=hidden name=hyds2>";
    Regex hiddenInput = new Regex("<input.*?>", RegexOptions.IgnoreCase);
    Regex value = new Regex(@"(?<=value=).*?(?=\s|>)");
    
    MatchCollection mc = hiddenInput.Matches(html);
    foreach (Match m in mc)
    {
        Console.WriteLine(value.Match(m.Value));
    }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Saturday, March 10, 2012 8:10 PM
    • Marked as answer by Bob Shen Wednesday, March 21, 2012 2:29 AM
    Saturday, March 10, 2012 8:09 PM

All replies

  • It might be more helpful to show what types of input, which I am assuming is what you have in your code snippet and under the eg: tag, even though the code smippet has a different name, and what you want the result to be.

    For instance, are you just wanting to remove the input tag all together, are you wanting to remove the value, but leave everything else in place, is this for a spcifically named input or is it all hidden inputs?  We need more information to assist you.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Saturday, March 10, 2012 6:38 PM
  • Here is a code:

    string html = "<INPUT type=hidden name=hyds2 value=1234><input name=hyds2 type=hidden value=1235><INPUT value=1236 type=hidden name=hyds2>";
    Regex hiddenInput = new Regex("<input.*?>", RegexOptions.IgnoreCase);
    Regex value = new Regex(@"(?<=value=).*?(?=\s|>)");
    
    MatchCollection mc = hiddenInput.Matches(html);
    foreach (Match m in mc)
    {
        Console.WriteLine(value.Match(m.Value));
    }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Saturday, March 10, 2012 8:10 PM
    • Marked as answer by Bob Shen Wednesday, March 21, 2012 2:29 AM
    Saturday, March 10, 2012 8:09 PM
  • Do you want to do this in C#? Looks like it's possibly best done in javascript, but not knowing what you are trying to do I could be completely wrong. :)

    Regards David R
    ---------------------------------------------------------------
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Saturday, March 10, 2012 10:07 PM