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 = "my friends passport numbers are V123456, V123457 and V123458";
  9. String rx = "(?:\\G(?!^)|\\bpassport\\b).*?\\b([a-zA-Z]{0,2}\\d{6,12}[a-zA-Z]{0,2})\\b";
  10. Pattern pattern = Pattern.compile(rx, Pattern.DOTALL);
  11. Matcher matcher = pattern.matcher(s);
  12. while (matcher.find()){
  13. System.out.println(matcher.group(1));
  14. }
  15. }
  16. }
Success #stdin #stdout 0.1s 35024KB
stdin
Standard input is empty
stdout
V123456
V123457
V123458