fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. // your code goes here
  15. String line = "C3H20ZnO2ABCD";
  16. String pattern = "([A-Z][a-z]*)(((?=[A-Z][a-z]*|$))|\\d+)";
  17.  
  18. // Create a Pattern object
  19. Pattern r = Pattern.compile(pattern);
  20.  
  21. // Now create matcher object.
  22. Matcher m = r.matcher(line);
  23.  
  24. while (m.find( )) {
  25. System.out.print(m.group(1));
  26. if (m.group(2).length() == 0) {
  27. System.out.println(" 1");
  28. } else {
  29. System.out.println(" " + m.group(2));
  30. }
  31. }
  32. }
  33. }
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
C 3
H 20
Zn 1
O 2
A 1
B 1
C 1
D 1