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 &.
Example: The string "$foo& bar foo" with replace text foo would be "$foo& bar $foo&".
So the replace text must be escaped using $ and & but not if the escape is already applied. Currently I'm using the regular expression ([^\\$]|^)text([^&]|$) 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 ([^\\$]|^)foo([^&]|$) doesn't matches but I want a not match only if both sign are found.
foo's should be replaced by$foo&provided the string is not already$foo&right?