title: Replace text in string with exceptions
body: <p>I want to replace some text in a java string, but I don't want to replace it if the text is inside a section defined by $ and &amp;.
  Example: The string "$foo&amp; bar foo" with replace text foo would be "$foo&amp; bar $foo&amp;".
  So the replace text must be escaped using $ and &amp; but not if the escape is already applied. Currently I'm using the regular expression <code>([^\\$]|^)text([^&amp;]|$)</code> and it works quite well but if only one of the symbols is found the regex doesn't match.
  Example: for the text "bar $foo" the regular expression <code>([^\\$]|^)foo([^&amp;]|$)</code> doesn't matches but I want a not match only if <strong>both</strong> sign are found.</p>