in reply to (tye)Re: How to gracefully deal with a regex problem
in thread How to gracefully deal with a regex problem

How would I use index() in this situation? Check for the presence of particular problematic characters?

The regex I'm using, as I clarified above isn't really a test per se - it's a part of the script which allows users to edit their data - using the form page as a template, and replacing particular lines in the HTML code with values they entered in the first place. Maybe there are better ways to do it than the following:

#Read in the template HTML file with the form . $form_file = $fields{formpage}; open (FILE, "$form_file"); @form = <FILE>; $select_flag=0; foreach (@form) { foreach $key (keys %fields) { if ($_ =~ /name=\"$key\"/i) { if (($key ne "table")&&($key ne "formpage")&&($key ne "dbname" +)&&($_ !~ /meta/i)) { if ($_ =~ m/check|radio/i) { $valueplace = index($_, "value="); $value = substr($_,$valueplace+7); ($value,$junk) = split(/\"/,$value); if ($fields{$key} eq $value) { $_ =~ s/input/input checked/i; print; print "\n"; $form_flag=1; last; } next; } elsif ($_ =~ /textarea/i) { #these are the regex giving me trouble if the data has wierd charact +ers $_ =~ s/><\/textarea/>$fields{$key}<\/textarea/i; } elsif ($_ =~ m/select/i) { } else { $_ =~ s/input/input value=\"$fields{$key}\"/i; } print; print "\n"; $form_flag = 1; } } elsif ($_ =~ /select/) { $select_flag=1; } elsif (($select_flag)&&($_ =~ m/option value=/i)) { $_ =~ s/option value=\"$fields{$key}\"/option value=\" +$fields{$key}\" selected/;; } } if (!$form_flag) { print; print "\n"; next; } else {$form_flag=0; next;} } }
BTW, this script doesn't entirely work the way I'd like it to. For some reason, it can't seem to do the menu selection right. Otherwise, it works pretty well.

Replies are listed 'Best First'.
(tye)Re2: How to gracefully deal with a regex problem
by tye (Sage) on Mar 07, 2001 at 02:32 UTC

    Well, I wasn't thinking of the right-hand part of a regex because:

    $problem= "["; $str= "(input)"; $str =~ s/input/input value="$problem"/; print "<$str>\n"; # produces <(input value="[")>

    So I don't see how your original problem matches this problem.

    I was thinking of the fact that people surprisingly often write $str =~ /\Q$sub\E/ instead of index($str,$sub).

    Now that you give us more information, it looks to me like you need to be using CGI.pm which will know how to escape your parameter values properly.

            - tye (but my friends call me "Tye")