http://qs321.pair.com?node_id=1091785


in reply to regex question: How do I modify in place in only one character class?

Putting aside the very odd goal...

The following regex will work for you.

By using a look behind assertion \K and a look ahead assertion, we can simply inject " selected" into the appropriate option:
$selection =~ s/<option [^>]*\K(?=>\s*\Q$value\E\b)/ selected/;

Replies are listed 'Best First'.
Re^2: regex question: How do I modify in place in only one character class?
by aturtle (Novice) on Jul 01, 2014 at 17:47 UTC
    Thank you! (And thank you for putting aside the odd nature of the program) I saw the \K discussed as a way to do a variable width look behind on page 249 of the camel book but was not grasping it. Also I need to read up on how to use '?'. It appears '^>'is finding and holding the insert place just before the last '>' and the '?=' is marking the search spot for the '\s*\Q$value\E' ? thanks again!