Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting string from within round brackets in Java with regex

I'm trying to extract a string from round brackets.
Let's say, I have John Doe (123456789) and I want to output the string 123456789 only.

I have found this link and this regex:

/\(([^)]+)\)/g

However, I wasn't able to figure out how to get the wanted result.

Any help would be appreciated. Thanks!

like image 736
Nir Tzezana Avatar asked Nov 17 '25 18:11

Nir Tzezana


1 Answers

String str="John Doe (123456789)";
System.out.println(str.substring(str.indexOf("(")+1,str.indexOf(")")));

Here I'm performing string operations. I'm not that much familiar with regex.

like image 144
SatyaTNV Avatar answered Nov 20 '25 08:11

SatyaTNV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!