Search code examples
javaregexcapturing-group

Java Regex: replace with capturing group


I have a string like this:

123456-1/1234/189928/2323 (102457921)

I want to get 102457921. How can I achieve it with regex?

I have tried:

"123456-1/1234/189928/2323 (102457921)".replaceAll("(\\.*\()(\d+)(\))","$2");

But it does not work. Any hints?


Solution

  • How about

    "123456-1/1234/189928/2323 (102457921)".replaceAll(".*?\\((.*?)\\).*", "$1");