I have a String in Java that holds data in this format:
String x = "xxxxxx xxxxxx xxxxxx (xxx)";
How can I set the value of that string to just be the characters in the brackets, without including the brackets? (Note that the character sizes will vary in each instance).
The one-line solution is to use
String.replaceAll()and the appropriate regex that captures the whole input (effectively replacing the whole input), but also captures (non-greedily) the part you want as group 1, then puts back just that group:FYI, the double back-slashes in the regex String is a single slash in regex, which then escapes the literal brackets in the regex
Here’s some test code:
Output: