tags:

views:

1146

answers:

2

Hi, 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>

.....

Thanks.

+1  A: 

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.

Any specific reason for trying to parse HTML with a regex rather than loading it into a DOM or using the DOM available in the browser through Javascript?

If you only have a snippet like that, you can use

value="(\d*)"

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

<select.*>(.*?)</select>

in an outer loop would work in most cases. Nevertheless, the DOM is your friend and avoids hacks like this.

Godeke
A: 

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.

Evan Fosmark