title: Perl regex substitution not working with global modifier
body: <p>I have code that looks like the following:</p>
  
  <pre><code>s/(["\'])(?:\\?+.)*?\1/(my $x = $&amp;) =~ s|^(["\'])(.*src=)([\'"])\/|$1$2$3$1.\\$baseUrl.$1\/|g;$x/ge
  </code></pre>
  
  <p>Ignoring the last bit (and only leaving the part where the problems occur) the code becomes:</p>
  
  <pre><code>s/(["\'])(?:\\?+.)*?\1/replace-text-here/g
  </code></pre>
  
  <p>I have tried using both, but I still get the same problem, which is that even though I am using the g modifier, this regex only matches and replaces the first occurrence. If this is a Perl bug, I don't know, but I was using a regex that matches everything between two quotes, and also handles escaped quotes, and I was following <a href="http://blog.stevenlevithan.com/archives/match-quoted-string" rel="nofollow">this blog post</a>. In my eyes, that regex should match everything between the two quotes, then replace it, then try and find another instance of this pattern, because of the g modifier.</p>
  
  <p>For a bit of background information, I am not using and version declarations, and strict and warnings are turned on, yet no warnings have shown up. My script reads an entire file into a scalar (including newlines) then the regex operates directly on that scalar. It does seem to work on each line individually - just not multiple times on one line. Perl version 5.14.2, running on Cygwin 64-bit. It could be that Cygwin (or the Perl port) is messing something up, but I doubt it.</p>
  
  <p>I also tried another example from that blog post, with atomic groups and possessive quantifiers replaced with equivalent code but without those features, but this problem still plagued me.</p>
  
  <p>Examples:</p>
  
  <pre><code>&lt;?php echo ($watched_dir-&gt;getExistsFlag())?"":"&lt;span class='ui-icon-alert'&gt;&lt;img src='/css/images/warning-icon.png'&gt;&lt;/span&gt;"?&gt;
  Should become (with the shortened regex):
  &lt;?php echo ($watched_dir-&gt;getExistsFlag())?replace-text-here:replace-text-here?&gt;
  Yet it only becomes:
  &lt;?php echo ($watched_dir-&gt;getExistsFlag())?replace-text-here:"&lt;span class='ui-icon-alert'&gt;&lt;img src='/css/images/warning-icon.png'&gt;&lt;/span&gt;"?&gt;
  
  &lt;?php echo ($sub-&gt;getTarget() != "")?"target=\"".$sub-&gt;getTarget()."\"":""; ?&gt;
  Should become:
  &lt;?php echo ($sub-&gt;getTarget() != replace-text-here)?replace-text-here.$sub-&gt;getTarget().replace-text-here:replace-text-here; ?&gt;
  And as above, only the first occurrence is changed.
  </code></pre>
  
  <p>(And yes, I do realise that this will spark into some sort of - don't use regex for parsing HTML/PHP. But in this case I think that regex is more appropriate, as I am not looking for context, I am looking for a string (anything within quotes) and performing an operation on that string - which is regex.)</p>
  
  <p>And just a note - these regexes are running in an eval function, and the actual regex is encoded in a single quoted string (which is why the single quotes are escaped). I will try any presented solution directly though to rule out my bad programming.</p>
  
  <p>EDIT: As requested, a short script that presents the problems:</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use strict;
  
  my $data = "this is the first line, where nothing much happens
  but on the second line \"we suddenly have some double quotes\"
  and on the third line there are 'single quotes'
  but the fourth line has \"double quotes\" AND 'single quotes', but also another \"double quote\"
  the fifth line has the interesting one - \"double quoted string 'with embedded singles' AND \\\"escaped doubles\\\"\"
  and the sixth is just to say - we need a new line at the end to simulate a properly structured file
  ";
  my $regex = 's/(["\'])(?:\\?+.)*?\1/replaced!/g';
  my $regex2 = 's/([\'"]).*?\1/replaced2!/g';
  
  print $data."\n";
  $_ = $data; # to make the regex operate on $_, as per the original script
  eval($regex);
  print $_."\n";
  $_ = $data;
  eval($regex2);
  print $_; # just an example of an eval, but without the fancy possessive quantifiers
  </code></pre>
  
  <p>This produces the following output for me:</p>
  
  <pre><code>this is the first line, where nothing much happens
  but on the second line "we suddenly have some double quotes"
  and on the third line there are 'single quotes'
  but the fourth line has "double quotes" AND 'single quotes', but also another "double quote"
  the fifth line has the interesting one - "double quoted string 'with embedded singles' AND \"escaped doubles\""
  and the sixth is just to say - we need a new line at the end to simulate a properly structured file
  
  this is the first line, where nothing much happens
  but on the second line "we suddenly have some double quotes"
  and on the third line there are 'single quotes'
  but the fourth line has "double quotes" AND 'single quotes', but also another "double quote"
  the fifth line has the interesting one - "double quoted string 'with embedded singles' AND \"escaped doubles\replaced!
  and the sixth is just to say - we need a new line at the end to simulate a properly structured file
  
  this is the first line, where nothing much happens
  but on the second line replaced2!
  and on the third line there are replaced2!
  but the fourth line has replaced2! AND replaced2!, but also another replaced2!
  the fifth line has the interesting one - replaced2!escaped doubles\replaced2!
  and the sixth is just to say - we need a new line at the end to simulate a properly structured file
  </code></pre>