fork download
  1. import java.util.regex.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String str = "Now you will see the following links for the items:\n" +
  9. "1111 leading 4 digits and then some text\n" +
  10. "2565 leading 4 digits and then some text\n" +
  11. "8978 leading 4 digits and then some text\n" +
  12. "foo\n" +
  13. "bar";
  14. Pattern p = Pattern.compile("(?m)^.*?(\\r?\\n)(?:^\\d{4}\\s.*?\\1)+");
  15. Matcher m = p.matcher(str);
  16. if (m.find())
  17. System.out.printf("Matched: %s%n", m.group(0));
  18.  
  19. }
  20. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
Matched: Now you will see the following links for the items:
1111 leading 4 digits and then some text
2565 leading 4 digits and then some text
8978 leading 4 digits and then some text