fork(1) 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("p\\.?o\\.?\\s*box \\d+(\\z|\\s)");
  14. //p.*o.*box \\d+(\\z|\\s)
  15. Pattern p = Pattern.compile("p.*o.*box \\d+(\\z|\\s)");
  16. Matcher m = p.matcher(str);
  17. int count =0;
  18. while(m.find()) {
  19. count++;
  20. System.out.println("Match: "+m.group(0));
  21. System.out.println("Match number "+count);
  22. System.out.println("start(): "+m.start());
  23. System.out.println("end(): "+m.end());
  24. }
  25.  
  26. str = "po box 1011";
  27. p = Pattern.compile("p\\.?o\\.?\\s*box \\d+(\\z|\\s)");
  28. m = p.matcher(str);
  29. count =0;
  30. while(m.find()) {
  31. count++;
  32. System.out.println("Match: "+m.group(0));
  33. System.out.println("Match number "+count);
  34. System.out.println("start(): "+m.start());
  35. System.out.println("end(): "+m.end());
  36. }
  37. }
  38. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Match: p.o. box 12456 
Match number 1
start(): 10
end(): 25
Match: po box 1011
Match number 1
start(): 0
end(): 11