fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String[] strings = { "987abc<*(123", "", "123", "test", "abc123" };
  12. Pattern pattern = Pattern.compile("(\\d)\\d*\\D*$");
  13.  
  14. for (String s : strings) {
  15. Matcher matcher = pattern.matcher(s);
  16. if (matcher.find()) {
  17. System.out.printf("'%s' --> %d\n", s, matcher.start(1));
  18. continue;
  19. }
  20. System.out.printf("'%s' --> %d\n", s, 0);
  21. }
  22. }
  23. }
Success #stdin #stdout 0.08s 48680KB
stdin
Standard input is empty
stdout
'987abc<*(123' --> 9
'' --> 0
'123' --> 0
'test' --> 0
'abc123' --> 3