Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by aturtle (Novice)
on Jul 01, 2014 at 00:39 UTC ( #1091782=perlquestion: print w/replies, xml ) Need Help??

aturtle has asked for the wisdom of the Perl Monks concerning the following question:

I have a string of html select options that looks like $selection in the code below. I have a variable $value might be in the list of html options in the form of $value--$hidden_value. If $value matches like in the code below I would like to 'preselect' the html select box for the user. The following code works, however it is not the way I was trying to do it. I was trying to use one regular expression instead of two like in $selection2, using a sub match $2 to just modify "> to be " selected > in place if $value matched before. How this possible? I have several perl books I have been looking in the camel book around page 200, as well as modern perl and on line did not come up with anything that worked better yet (but I did get this far) Thanks in advance!
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my ($selection,$selection2,$selection3,$value,$t2); $value = "13gr"; print "regex question: How do I modify in place in only one character +class?\n"; $selection = qq| <option value="000-F--10180">000-F <option value="11EG--10181">11EG <option value="12160--10182">12160 <option value="12164--10185">12164 <option value="12170--10186">12170 <option value="12216--12216">12216 <option value="12330--12330">12330 <option value="12480--12480">12480 <option value="12484--12484">12484 <option value="12488--12488">12488 <option value="12534--12534">12534 <option value="12756--12756">12756 <option value="12760--12760">12760 <option value="12764--12764">12764 <option value="12813--12813">12813 <option value="12A--10983">12A <option value="12B--10987">12B <option value="12BB--10347">12BB <option value="12D--10993">12D <option value="12ea--10376">12ea <option value="12JI--10438">12JI <option value="12NO--10242">12NO <option value="13gr--11117">13gr <option value="2--2--11070">2--2 <option value="22QS--33337">22QS <option value="22WO--55555">22WO <option value="22WO.1--64444">22WO.1 |; $selection2 = $selection ; $selection3 = $selection ; $selection =~ s/(\Q$value\E)/$1 selected/ ; $selection2 =~ s/((?:.*?($value)){2}).*(\">)// ; $selection3 =~ s/(?:.*?($value)){2}// ; my $t3 = $&; #print "Matches: $1, $2, $3, $4, $5, $6, $7\n"; ($t2 = $t3) =~ s/(\">)/\" selected >/ ; $selection3 .= $t2 ; print "3: $selection3\n\n"; print "value $value\n"
PS the above code is just a mockup just for an example
  • Comment on regex question: How do I modify in place in only one character class?
  • Download Code

Replies are listed 'Best First'.
Re: regex question: How do I modify in place in only one character class?
by wind (Priest) on Jul 01, 2014 at 01:10 UTC

    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/;
      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1091782]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-01-30 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?