in reply to replacing equal to operator
You don't need all those double quotes in the regexes. On the left-hand side, they're being taken literally, so just remove them, and on the right-hand side, you need them because of the /e modifier, but since you're not actually making use of that, get rid of the /e modifier and the double quotes on the right-hand side. In other words, s/=/="/ and s/\n/"\n/ (or s/$/"/ for the latter). No need for the /gs modifiers either.
However, the solution as you've currently written it seems brittle to me: In particular, it'll replace any equals sign. Personally, I'd either see if there is an existing module to parse whatever file format this is, and if there isn't, I'd try to parse the values myself, maybe something like the following. Note how you can extend the regex to do more, like for example I've designed it so that it won't add double quotes if there are already double quotes around the string, and reject the input if those double-quotes are unbalanced.
use warnings; use strict; use Test::More; my $string=qq{parameter=TO_DATE('1900-01-01','YYYY-MM-DD')\n}; my $REGEX = qr{^ (\w+) = (?| "(.+)" | ( (?!").+(?<!") ) ) (\n|\z) }x; if ( $string =~ $REGEX ) { $string = qq{$1="$2"$3} } else { die "failed to parse $string" } is $string, qq{parameter="TO_DATE('1900-01-01','YYYY-MM-DD')"\n}; done_testing;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: replacing equal to operator
by ty_sopw (Novice) on Jan 28, 2021 at 19:20 UTC |