fork(2) download
  1. import java.util.regex.*;
  2.  
  3. class Main {
  4. public static void main(String[] args) {
  5. String text = " this xx(yyzz(iijj))qq((kkll)(gghh)) is there any way in which i could match \"xx(yyzz(iijj))\" and \"qq((kkll)(gghh))\" separately using";
  6. String regex = "\\(([^()]*+|\\([^()]*+\\))*\\)";
  7. System.out.println("Text = " + text);
  8. System.out.println("Regex = " + regex);
  9. Matcher matcher = Pattern.compile(regex).matcher(text);
  10. while(matcher.find()) {
  11. System.out.print("entire match: '"+matcher.group()+"'");
  12. System.out.println(", from "+matcher.start()+" to "+matcher.end());
  13. for(int i = 1; i <= matcher.groupCount(); i++) {
  14. System.out.println("- group("+i+") = "+matcher.group(i));
  15. }
  16. }
  17. }
  18. }
Success #stdin #stdout 0.09s 212736KB
stdin
Standard input is empty
stdout
Text   =  this xx(yyzz(iijj))qq((kkll)(gghh)) is there any way in which i could match "xx(yyzz(iijj))" and "qq((kkll)(gghh))" separately using
Regex  = \(([^()]*+|\([^()]*+\))*\)
entire match: '(yyzz(iijj))', from 8 to 20
- group(1) = 
entire match: '((kkll)(gghh))', from 22 to 36
- group(1) = 
entire match: '(yyzz(iijj))', from 80 to 92
- group(1) = 
entire match: '((kkll)(gghh))', from 101 to 115
- group(1) =