Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline C# Regex to match after a blank line

Tags:

regex

I'm looking for a multiline regex that will match occurrences after a blank line. For example, given a sample email below, I'd like to match "From: Alex". ^From:\s*(.*)$ works to match any From line, but I want it to be restricted to lines in the body (anything after the first blank line).

Received: from a server
Date: today
To: Ted
From: James
Subject: [fwd: hi]

fyi

----- Forwarded Message -----
To: James
From: Alex
Subject: hi

Party!

like image 313
user10392 Avatar asked Nov 06 '22 23:11

user10392


1 Answers

I'm not sure of the syntax of C# regular expressions but you should have a way to anchor to the beginning of the string (not the beginning of the line such as ^). I'll call that "\A" in my example:

\A.*?\r?\n\r?\n.*?^From:\s*([^\r\n]+)$

Make sure you turn the multiline matching option on, however that works, to make "." match \n

like image 181
Loren Segal Avatar answered Nov 17 '22 11:11

Loren Segal