RegEx help determining value of select box (html)

Posted on 2012-09-16
Last Modified: 2012-09-21
Hey everyone,

I'm having trouble wrapping my head around this. I'm trying to parse through some html and get the selected value for a select box with the name of "status"

This is the select box code:

           <select name="status">
                <option value="1" >online</option>
                <option value="0" selected>offline</option>
               <option value="-1" >deleted</option>

Open in new window

Essentially, what I'm trying to get together is a function that will return the value of the selected item.

For example: sValue = GetSelectedItemFromSelectBox("status");

This would return: "0"

Can someone here give me a hand with this?
Question by:mcainc
    LVL 74

    Accepted Solution

    Try the following:

    static string GetSelectedItemFromSelectBox(string input, string elementNameAttributeValue)
        string select_pattern = "(?is)<select [^>]*name=[\"']?" + elementNameAttributeValue + "['\"]?.*?</select>";
        string value_pattern = "(?i)(?<=(?:[^<]*|<(?!select )))<option (?=[^>]*value=[\"']?([^ '\">]*))[^>]*selected";
        Match m = Regex.Match(input, select_pattern);
        if (m.Success)
            m = Regex.Match(m.Value, value_pattern);
            if (m.Success)
                return m.Groups[1].Value;
        return "";

    Open in new window


    Author Closing Comment

    Wow, this worked perfectly! Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now