slayer
slayer

Reputation: 445

Regex to match some pattern with line break

I want to write regEx to match following pattern:

From: ***********************
Sent: ***********************
To: ***********************
Subject: *******************

I wrote regEx as

.*From:.+(\n)Sent:.+(\n)To:.+(\n)Subject:.+(\n).*

But this is not working. Kindly help me as I am new to regEx.

Upvotes: 6

Views: 7855

Answers (4)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

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)); 
} 

Upvotes: 5

buræquete
buræquete

Reputation: 14688

You might use something like this;

From:.+\nSent:.+\nTo:.+\nSubject:.+

The regex you have given might fail since you do not need the starting .* since you want to capture from "From:" part.

If you only wish to capture the text;

From:(.+)\nSent:(.+)\nTo:(.+)\nSubject:(.+)

where

$1 -> From text

$2 -> Sent text

$3 -> To text

$4 -> Subject text

You can use any regex utilizing functions from whichever language using to get these captured groups

Upvotes: 0

shA.t
shA.t

Reputation: 16958

IMO you can use a regex like this:

/^(?=From: ).*([\r\n]|\r\n)(?=Sent: ).*([\r\n]|\r\n)(?=To: ).*([\r\n]|\r\n)(?=Subject: ).*$/gm

[Regex Demo]

Upvotes: 0

Jan
Jan

Reputation: 43169

This could be achieved as follows:

^From:\s.(?P<from>.+)\R   # look for "From:" at the beginning of the line/string,
                           # followed by a capturing group and `R`
Sent:\s(?P<sent>.+)\R      # same as above  
To:\s(?P<to>.+)\R
Subject:\s(?P<subject>.+)

The code works if you use PCRE (i.e. PHP, etc.).
See a demo here on regex101.com (with m and x flags).

Upvotes: 0

Related Questions