fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3.  
  4. class Ideone
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String s = "Ca(OH)2";
  9. Pattern p = Pattern.compile("\\([A-Za-z0-9]+\\)[0-9]*|[A-Za-z0-9]+");
  10. Matcher m = p.matcher(s);
  11. List<String> res = new ArrayList<>();
  12. while(m.find()) {
  13. res.add(m.group());
  14. }
  15. System.out.println(res);
  16. }
  17. }
Success #stdin #stdout 0.09s 51780KB
stdin
Standard input is empty
stdout
[Ca, (OH)2]