title: Replace a substring in a string except when the string is inside quotes
body: <p>Regex dialect: Java<br>
  The problem: given a string, replace all occurrences of a substring inside it, except when these occurrences are inside quotes.</p>
  
  <p>Example1:</p>
  
  <pre><code>string: "test substr 'test substr' substr"
  substring: "substr"
  replacement: "YYYY"
  output: "test YYYY 'test substr' YYYY"
  </code></pre>
  
  <p>Example2:</p>
  
  <pre><code>string: "test sstr 'test sstr' sstr"
  substring: "substr"
  replacement: "YYYY"
  output: "test sstr 'test sstr' sstr"
  </code></pre>
  
  <p>Example3:</p>
  
  <pre><code>string: "test 'test substr'"
  substring: "substr"
  replacement: "YYYY"
  output: "test 'test substr'"
  </code></pre>
  
  <p>This is my best try thus far:</p>
  
  <pre><code>Regex: ((?:[^']*'[^']+')*?[^']*?)substring
  Replace: $1replacement
  </code></pre>
  
  <p>The problem with it is that it needs a substring outside the quotes after the last string within the quotes otherwise it doesn't work, so Example3 will fail (output: "test 'test YYYY'").</p>
  
  <p>Many thanks for your help.</p>