Additionally to Rossiar's answer, if you don't want to use Pattern
and Matcher
classes and just want to use replaceAll
method then your code is working as expected, you have below string:
pro 300\nlocation in ("aaa","bbb")
^^^^^^^^^^^^^^^^^^^^^^^^^ and you replace this by "aaa","bbb"
So, your final string is:
pro 300\n"aaa","bbb"
String.replaceAll
If you want just to get "aaa","bbb"
using replaceAll
, you will have to match the complete string by using:
conditions = conditions.replaceAll(".*location in \\((.*?)\\).*", "$1");
^--------- Note ---------^
Or for your specific string you could use:
"pro 300\nlocation in (\"aaa\",\"bbb\")".replaceAll(".*\\((.*?)\\).*", "$1");
I can't test it right now if \n
is not being matched by .*
, so in case it isn't then you can replace multilines by using single line
flag or doing a regex trick:
Single line flag
"pro 300\nlocation in (\"aaa\",\"bbb\")".replaceAll("(?s).*\\((.*?)\\).*", "$1");
Working demo
Regex trick
"pro 300\nlocation in (\"aaa\",\"bbb\")".replaceAll("[\\s\\S]*\\((.*?)\\)[\\s\\S]*", "$1");
Working demo
location in ("aaa","bbb")
so only this part can be replaced leavingpro 300\n
untouched. Why do you expect result to be only"aaa","bbb"
(or did I misunderstand you)?location in ("aaa","bbb")
then replacing it with capture buffer 1, which is"aaa","bbb"
. The result ispro 300"aaa","bbb"
. Not sure what the\n
is doing there. If you want to get result of"aaa","bbb"
, you'd have to use something like.*?location in \\((.*?)\\).*