Why doesn\'t the following change the text for me in Android?
String content = \"test\\n=test=\\ntest\";
content = content.replaceAll(\"^=(.+)=$\", \"
The best way to deal with this is to set Pattern.MULTILINE. Using MULTILINE, ^ and $ will match on lines that are separated using only \n, and will similarly handle the beginning of input and the end of input.
Using String.replaceAll you need to set these within the pattern using an embedded flag expression (?m), for MULTILINE:
content = str.replaceAll("(?m)^=(.+)=$", "$1 ");
If you don't use MULTILINE, you need to use positive lookahead and lookbehind for the \n, and the regex gets complicated in order to match the first line, and the last line if there's no \n at the end, e.g. if our input is: =test=\n=test=\n=test=\n=test=.
String pattern = "(?<=(^|\n))=(.+)=(?=(\n|$))";
content = str.replaceAll(pattern, "$2 ");
In this pattern we're supplying options for the lookbehind: \n or beginning of input, (^|\n); and for the lookahead: \n or end of input, (\n|$). Notice that we need to use $2 as the captured group reference in the replacement because of the group introduced by the first or.
We can make the pattern more complicated by introducing the alternatives in the lookahead/lookbehind in non-capturing groups, which look like (?:):
String pattern = "(?<=(?:^|\n))=(.+)=(?=(?:\n|$))";
content = str.replaceAll(pattern, "$1 ");
Now we're back to using $1 as the captured group in the replacement.