[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


RegEx help determining value of select box (html)

Posted on 2012-09-16
Medium Priority
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 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 38406618
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

ID: 38421095
Wow, this worked perfectly! Thank you!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Suggested Courses

834 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