1

I am using the following regex "location in \\((.*?)\\)"

against the following string: pro 300\nlocation in ("aaa","bbb")

according to online regex test for java, the result should be "aaa","bbb" but when I run that in java code like this:

conditions.replaceAll("location in \\((.*?)\\)", "$1");

I get pro 300"aaa","bbb"

What I am doing wrong? Thanks in advance.

3
  • Your regex can match only part location in ("aaa","bbb") so only this part can be replaced leaving pro 300\n untouched. Why do you expect result to be only "aaa","bbb" (or did I misunderstand you)?
    – Pshemo
    Jun 29, 2015 at 21:10
  • The result is doing what you asked it to. Its matching location in ("aaa","bbb") then replacing it with capture buffer 1, which is "aaa","bbb". The result is pro 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 \\((.*?)\\).*
    – user557597
    Jun 29, 2015 at 21:36
  • Thanks sln, I am rather new with regular expressions and that was very helpful.
    – Nicolasllk
    Jun 30, 2015 at 12:05

2 Answers 2

7

replaceAll() is replacing the part of conditions matched by group(0) of your regex.

To retrieve only the part inside (...) you need to use:

Pattern p = Pattern.compile("location in \\((.*?)\\)");
Matcher m = p.matcher(conditions);
if (m.find())
{
    String s = m.group(1);
}

This will retrieve the inner bracket of your regular expression (.*?)

2
  • I think that this is a better regex for the case location in ((((?!)).)*)
    – Santiago
    Jun 29, 2015 at 21:22
  • Worked like a charm! Thanks!
    – Nicolasllk
    Jun 30, 2015 at 12:06
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.