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;

In reply to Re: replacing equal to operator by haukex
in thread replacing equal to operator by ty_sopw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.