I want to understand, is my expression in brackets. I create such regExp –
public static final String complexValue = "([-]?[(].+[)])"
But I have fail on such input string –
String st = "(4+6)+(3)"
Is there a way to create such regExp, that string (5+x) matches it and string (4+6)+(3) no.
Literal answer is yes:
"^\\(5\\+x\\)$"does what you asked, but not what you want.The problem is, it’s unclear what you want for
((4)+(6)): should it match? Do you want to allow potentially infinite nesting of parentheses, or just a pair of outer parentheses with no inner parentheses allowed?In the first case, your grammar is irregular, so regular expressions are unable to parse it. (I seem to recall that there are “irregular regular expression” dialects, but IIRC java implementation is not among them).
In the second case, something like
"^\\([^)]+\\)$"will do the job.