regular expression for extracting options inside select tag
I need to extract options in ``particular select tag. Is it possible to accomplish using one regex or I'll have to capture the inner html of select first and then the options? Here is an example of html:
<select id="select_id"> <option selected value="">Select Type</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select>
While it is possible to construct a regex that would do what you want, I really thing you would be happier doing it via the DOM, unless you have some reason to not use the DOM. There are no tags that suggest language or platform, so it is hard to get more specific than that.
If you only have a snippet like that, you can use
Where (\d*) will capture the values of each option.
The problem I see is that you would have to narrow your field of search via another regex to get to such a simple query. Something like
in an outer loop would work in most cases. Nevertheless, the DOM is your friend and avoids hacks like this.
I would look for some library DOM support, but if you must, do something similar to this:
"<select.*?>.*?<option value=\"(\d+)\">" + select_id + "</option>.*?</select>"
Where select_id is the option selection. Also, make sure you have multiline support enabled.