fork 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.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String str = "some text p.o. box 12456 Floor 105 streetName Street";
  13. Pattern p = Pattern.compile("(?i)\\bp\\.?\\s*o\\.?\\s*box\\s*(\\d+)(?:\\z|\\s)");
  14. Matcher m = p.matcher(str);
  15. int count =0;
  16. while(m.find()) {
  17. count++;
  18. System.out.println("Match: "+m.group(0));
  19. System.out.println("Digits: "+m.group(1));
  20. System.out.println("Match number "+count);
  21. System.out.println("start(): "+m.start());
  22. System.out.println("end(): "+m.end());
  23. }
  24.  
  25. }
  26. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
Match: p.o. box 12456 
Digits: 12456
Match number 1
start(): 10
end(): 25