多行C#正则表达式匹配空行后

时间:2022-09-30 22:28:40

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).

我正在寻找一个多行正则表达式,它将匹配空行后的出现次数。例如,给出下面的示例电子邮件,我想匹配“From:Alex”。 ^来自:\ s *(。*)$用于匹配任何From行,但我希望它仅限于正文中的行(第一个空白行之后的任何行)。

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

fyi

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

Party!

4 个解决方案

#1


2  

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:

我不确定C#正则表达式的语法,但你应该有一种方法来锚定到字符串的开头(而不是行的开头,如^)。我在我的例子中称之为“\ A”:

\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

确保你打开多线匹配选项,无论如何有效,使“。”匹配\ n

#2


0  

Writing complicated regular expressions for such jobs is a bad idea IMO. It's better to combine several simple queries. For example, first search for "\r\n\r\n" to find the start of the body, then run the simple regex over the body.

为这些工作编写复杂的正则表达式是一个坏主意IMO。最好结合几个简单的查询。例如,首先搜索“\ r \ n \ r \ n”以查找正文的开头,然后在正文上运行简单的正则表达式。

#3


0  

This is using a look-behind assertion. Group 1 will give you the "From" line, and group 2 will give you the actual value ("Alex", in your example).

这是使用后视断言。第1组将为您提供“从”行,第2组将为您提供实际值(在您的示例中为“Alex”)。

(?<=\n\n).*(From:\s*(.*?))$

#4


0  

\s{2,}.+?(.+?From:\s(?<Sender>.+?)\s)+?

The \s{2,} matches at least two whitespace characters, meaning your first From: James won't hit. Then it's just a matter of looking for the next "From:" and start capturing from there.

\ s {2,}匹配至少两个空白字符,这意味着你的第一个From:James不会命中。然后,这只是寻找下一个“从:”并从那里开始捕获的问题。

Use this with RegexOptions.SingleLine and RegexOptions.ExplicitCapture, this means the outer group won't hit.

与RegexOptions.SingleLine和RegexOptions.ExplicitCapture一起使用,这意味着外部组不会命中。

#1


2  

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:

我不确定C#正则表达式的语法,但你应该有一种方法来锚定到字符串的开头(而不是行的开头,如^)。我在我的例子中称之为“\ A”:

\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

确保你打开多线匹配选项,无论如何有效,使“。”匹配\ n

#2


0  

Writing complicated regular expressions for such jobs is a bad idea IMO. It's better to combine several simple queries. For example, first search for "\r\n\r\n" to find the start of the body, then run the simple regex over the body.

为这些工作编写复杂的正则表达式是一个坏主意IMO。最好结合几个简单的查询。例如,首先搜索“\ r \ n \ r \ n”以查找正文的开头,然后在正文上运行简单的正则表达式。

#3


0  

This is using a look-behind assertion. Group 1 will give you the "From" line, and group 2 will give you the actual value ("Alex", in your example).

这是使用后视断言。第1组将为您提供“从”行,第2组将为您提供实际值(在您的示例中为“Alex”)。

(?<=\n\n).*(From:\s*(.*?))$

#4


0  

\s{2,}.+?(.+?From:\s(?<Sender>.+?)\s)+?

The \s{2,} matches at least two whitespace characters, meaning your first From: James won't hit. Then it's just a matter of looking for the next "From:" and start capturing from there.

\ s {2,}匹配至少两个空白字符,这意味着你的第一个From:James不会命中。然后,这只是寻找下一个“从:”并从那里开始捕获的问题。

Use this with RegexOptions.SingleLine and RegexOptions.ExplicitCapture, this means the outer group won't hit.

与RegexOptions.SingleLine和RegexOptions.ExplicitCapture一起使用,这意味着外部组不会命中。