0

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?

2
  • split with whitespace and remove braces? Commented May 2, 2014 at 13:28
  • of course, but I'd like a regex solution. Commented May 2, 2014 at 13:29

5 Answers 5

5

How about

"123456-1/1234/189928/2323 (102457921)".replaceAll(".*?\\((.*?)\\).*", "$1");
Sign up to request clarification or add additional context in comments.

2 Comments

What does .*? mean?
It is the same as .*, except that it is non-greedy. You can read more here.
1

Well, you can do this:

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

1 Comment

@sshashank124, yes. Changed + to *
0

You could do it as:

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

Comments

0

What about a "double" replaceAll regex simplified one

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

Comments

0

You can try something like that:

var str = '123456-1/1234/189928/2323 (102457921)';
    console.log(str.replace(/[-\d\/ ]*\((\d+)\)/, "$1"));
    console.log((str.split('('))[1].slice(0, -1));
    console.log((str.split(/\(/))[1].replace(/(\d+)\)/, "$1"));
    console.log((str.split(/\(/))[1].substr(-str.length - 1, 9));
    console.log(str.substring(str.indexOf('(') + 1, str.indexOf(')')));

In other case you must be familiar with the specifics of the input data to generate a suitable regexp.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.