This is my simplified regex:
abc\((. ?)\)
I want to match like:
abc(wwww)). abc(wwww)).
See the result
I want to match the ) and everything after that at end of first match.
Examples:
?text[.ev.wa.vew.av.ewa]=>.ev.wa.vew.av.ewa?text[hello$(abc)elv]=>hello$(abc)elv?text(just test[test])=>just test[test]?text(test 2[test]here something)=>test 2[test]here something
CodePudding user response:
You can use
\?text(?:\[([^\]\[]*)]|\(([^()]*)\)).
See the regex demo. The results are either in Group 1 or Group 2 (you will need to check in the code).
Details
\?text- a?texttext(?:\[([^\]\[]*)]|\(([^()]*)\))- one of the two alternatives:`\[([^\]\[]*)-[, zero or more chars other than[and]and then a]char|- or\(([^()]*)\)-(, zero or more chars other than(and)and then a)char
.- one or more chars other than line break chars as many as possible.
Now, if you need to support nested brackets, you would need a recursion support, but Java regex does not provide such an option. Hence, the only regex way is to hardcode nested level support. Here is an example with one nested level:
\?text(?:\[([^\]\[]*(?:\[[^\]\[]*][^\]\[]*)*)]|\(([^()]*(?:\([^()]*\)[^()]*)*)\)).
See the regex demo. You can add more levels.
CodePudding user response:
after reading the comments below try this: abc\(([^)] \)\([^)] \))
https://regex101.com/r/lC8UJb/2
using negated character classes like [^)] is a little safer thank .*? as it is more exact to what is usually desired.
