Regex - match a select list's selected option's text and value


Trying to extract the selected option from a select list such as:

<select name="chk_prequalify"><option value="">(select)</option><option value="N">No</option><option value="Y" selected>Yes</option></select>

The regular expression of:
<select [^>]*(?<=name="chk_prequalify"[^<]*)>.*?<option[^>]*selected.*?>(.*?)</option>.*?</select>
works great, so long as that select list has "selected" in one of the options. If I don't have a "selected" in the text to search, it goes down to the next occurrence of where it finds selected (possibly WAY down) and grabs that.

So my question is, using the select list shown above:
How can I extract the "Yes" for selected? And,
How can I extract the "Y" for the value of that selected option?

These would of course be two different regular expressions. But I do need to be able to accommodate for instances where there is no "selected", and the regular expression will simply not match, instead of matching something way down further from another select list.

Thanks for your help!
aaron900Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
But I do need to be able to accommodate for instances where there is no "selected", and the regular expression will simply not match

Can you clarify this? If it is going down to the next match, then it is not matching the first one!
0
Terry WoodsIT GuruCommented:
Try this pattern:

<select [^>]*name="chk_prequalify"[^>]*>(?:(?!</select>).)*?<option(?=[^>]*selected)[^>]*value\s*=\s*"([^"]*)"[^>]*>(.*?)</option>

My result:
  [0] => Array
        (
            [0] => <select name="chk_prequalify"><option value="">(select)</option><option value="N">No</option><option value="Y" selected>Yes</option>
        )

    [1] => Array
        (
            [0] => Y
        )

    [2] => Array
        (
            [0] => Yes
        )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Terry WoodsIT GuruCommented:
Note that I've changed a key part of the pattern where there was:
.*?
to:
(?:(?!</select>).)*?
which should prevent the match looking forward to a select further down the page.
0
aaron900Author Commented:
This is perfect! Thanks for the quick and accurate response! These lookarounds definitely take some getting used to!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.