in reply to How to use a regex to parse html form tags
The second statement for "$orgtext" is equivalent to:The above works. I reduced it to it's simplest form as a sanity check. Then I tried:$orgtext = Whey; # this one right here $newtext = Popcorn;$orgtext = /[Ww]hey/; # this one right here $newtext = Popcorn;
If nothing was ever assigned to $_ at the time your second $orgtext statement executed, you get the warning because perl is doing a regex match on $_, and $_ is undefined because no value has been assigned to it.$orgtext = ( $_ =~ /[Ww]hey/ );
Maybe that second example should have been something like this?
$orgtext = qr/[Wwhey]/;
As for what you "eventually want to try":
$orgtext = /<form[.*]?*\/form>/; # this one right here $newtext = bloc +k;
That assignment to $orgtext has not only the same problem cited above (use "qr/.../" instead?), but also problems with improper use of the square brackets, period, question mark and asterisks. I think what you intended was something like this:
$orgtext = qr/<form.*?\/form>/sg; # s is needed so that "." matches ne +wline
But as others have pointed out, an HTML parsing module, once you get the hang of it, is probably a better approach.
Next there's this in your "base code":
The comment about "that can easily appear in a binary" makes no sense, and the use of "m" on that substitution is actually pointless -- it would be relevant if you were anchoring the regex match with "^" or "$", because "m" alters the behavior of those anchors, but you're not using them. (The "whey" example doesn't use "." either, so the "s" is pointless as well in that case, but it's harmless, and I know you eventually expected to use ".")$intext =~ s/$orgtext/$newtext/ms; # the ms is for coping correctly with newlines (that can easily appear + in a binary). ... # replaces ALL occurrences of orgtext with newtext and places the numb +er of occurences in $count
And then, based on your closing comment (which refers to a "$count" variable that doesn't appear anywhere), it looks like you should have used the "g" modifier on that substitution, so that all occurrences of "$orgtext" would be changed to "$newtext".
|
|---|