Just do something like this

my $string = q/aab"123"bcc abc="123" abc="345" aab"123"bcc/; $string =~ s/(abc="123")/$1 aabbcc/; # or better (doesn't do unnecessary replacing) $string =~ s/(?<=abc="123")/ aabbcc/; __END__ Benchmark: timing 300000 iterations of lookback, normal... lookback: 4 wallclock secs ( 4.32 usr + 0.01 sys = 4.33 CPU) @ 69 +348.13/s (n=300000) normal: 6 wallclock secs ( 6.01 usr + 0.02 sys = 6.03 CPU) @ 49 +759.50/s (n=300000) Rate normal lookback normal 49759/s -- -28% lookback 69348/s 39% --

A suggestion for the next time, post some code (and the error message you get) so that we can see what you have so far ...

Update: AM is right, the positive lookahead was wrong. I replaced it with a lookbehind and now it works. To show the benefit I also included a small benchmark. As is always the case with benchmarks - results vary depending on the length of the text being replaced.

-- Hofmator


In reply to Re: How do I search and replace a string with quotes by Hofmator
in thread How do I search and replace a string with quotes by Hammy

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.