简体   繁体   English

使用正则表达式获取正文,不包括特定条件

[英]Grabbing a body of text using regex excluding specific conditions

I'm using Python regex to grab the body of a parsed email which may contain nothing or may look something like this:我正在使用 Python 正则表达式来获取已解析的 email 的主体,它可能不包含任何内容或可能看起来像这样:

Some coherent sentence.一些连贯的句子。

lalskjfa;ljkd lalskjfa;ljkd

the other stuff其他的东西


A couple of lines of email signature blah blah几行 email 签名等等

blah blah blah等等等等等等


I want everything in that body of the email EXCLUDING the signature line opener and its contents.我想要 email 主体中的所有内容,不包括签名行开启器及其内容。

I'm basically tearing out everything but that signature email in order to reformat it for reporting.我基本上撕掉了除签名 email 之外的所有内容,以便重新格式化以进行报告。

I've tried:我试过了:

  • negative lookahead: \G(\A\z|.*\n*(?!_))负前瞻: \G(\A\z|.*\n*(?!_))

  • positive lookahead: \G(\A\z|.*\n*(?=_))正向前瞻: \G(\A\z|.*\n*(?=_))

Neither seems to be doing the trick.两者似乎都没有奏效。

With a negative lookahead, it seems to be grabbing everything.以消极的前瞻性,它似乎抓住了一切。 With a positive lookahead, it seems to be grabbing nothing.有了积极的展望,它似乎什么也没抓住。

The output I'm hoping to achieve is this text:我希望实现的 output 是这个文本:

Some coherent sentence.一些连贯的句子。

lalskjfa;ljkd lalskjfa;ljkd

the other stuff其他的东西

You may use您可以使用

(?s)\A.*?(?=\n_)

It matches它匹配

  • (?s) - re.DOTALL inline flag (?s) - re.DOTALL内联标志
  • \A - start of string \A - 字符串的开头
  • .*? - any 0+ chars, as few as possible till the first occurrence of - 任何 0+ 字符,尽可能少,直到第一次出现
  • (?=\n_) - a newline followed with _ char. (?=\n_) - 后跟_字符的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM