Regex to match some pattern with line break

Your regex does not work because of two possible reasons:

  • The newline sequence can be \r\n, or \r, or \n (or even more, \u000B, \u000C, \u0085, \u2028 or \u2029), but you only coded in the LF. Adding an optional CR (carriage return, \r) can help.
  • Also, after Subject:..., there is no newline, so you need to remove it.
  • In Java 8+, there is a special line break shorthand class, \R, that you may use to match any line break sequence.

You can use

From:.+\r?\nSent:.+\r?\nTo:.+\r?\nSubject:.+
From:.+\RSent:.+\RTo:.+\RSubject:.+

Search for a partial match with Matcher#find().

See the regex demo

And the IDEONE demo:

String p = "From:.+\r?\nSent:.+\r?\nTo:.+\r?\nSubject:.+"; 
// String p = "From:.+\\RSent:.+\\RTo:.+\\RSubject:.+";  // Java 8+ compliant
String s = "Some text before.....\r\nFrom: ***********************\r\nSent: ***********************\r\nTo: ***********************\r\nSubject: *******************"; 
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(0)); 
} 

Tags:

Java

Regex