Arya
Arya

Reputation: 8985

preventing regex to capture too early

I have the following text which I will be running regex on. I want to match the "selected" option and get its value. In this example Haiti is the selected option.

<option value="502">Guatemala (+502)</option><option value="224">Guinea (+224)</option><option value="245">Guinea-Bissau (+245)</option><option value="592">Guyana (+592)</option><option value="509" selected >Haiti (+509)</option><option value="504">Honduras (+504)</option><option value="852">Hong Kong (+852)</option>

I tried this regex

<option value="(.*?)" selected >

which matches

224">Guinea (+224)</option><option value="245">Guinea-Bissau (+245)</option><option value="592">Guyana (+592)</option><option value="509

I want to match "509" only. Any solutions for this?

Upvotes: 0

Views: 57

Answers (1)

Thomas
Thomas

Reputation: 88707

You're matching everything from the first <option value=", try to not match the quotes instead, i.e. <option value="([^"]*)" selected > (meaning anything but double quotes is allowed between the double quotes :) ).

Upvotes: 2

Related Questions