简体   繁体   English

正则表达式/ ^主题和/ ^来自同一时间

[英]Regex on /^Subject and /^From at the same time

Where is the mistake in this regex and how do I possibly match two conditions at the same time for both subject field and from field? 这个正则表达式中的错误在哪里?如何同时为主题字段和字段匹配两个条件? The subject is "empty line" (there is nothing written in the subject field) and the email comes from "assistant@mail.com" then accept mail: 主题是“空行”(主题字段中没有任何内容),电子邮件来自“assistant@mail.com”,然后接受邮件:

/^From:(.*)assistant\@gmail\.com|^Subject:\s*$/ OK

Would be really thankful for any help at all. 非常感谢任何帮助。

I made an assumption about your text and how it is laid out. 我假设你的文字及其如何布局。

Does this work? 这有用吗?

/(From:\\sassistant@gmail\\.com)|(Subject:.*)$/

I capture the email in one group or match the empty subject in another group, assuming the text you're matching is one line long. 我在一个组中捕获电子邮件或匹配另一个组中的空主题,假设您匹配的文本是一行长。

See here for the match: https://regex101.com/r/CP0jfc/2/ 请看这里的比赛: https//regex101.com/r/CP0jfc/2/

May be you get some idea from this. 也许你从中得到一些想法。

^From:\s*assistant\@gmail\.com|^Subject:\s*$

Demo:: https://regex101.com/r/zDzbyX/1 演示:: https://regex101.com/r/zDzbyX/1

Assuming you have multiple lines, one option could be to first match the From part and then match 0+ lines in between and then match the Subject part. 假设您有多行,一个选项可能是首先匹配From部分,然后匹配其中的0+行,然后匹配Subject部分。

If it is the otherway around, you could turn the logic around imthe pattern. 如果它是相反的,你可以围绕模式转变逻辑。

^From:.*?assistant@gmail\.com.*(?:\n.*)*\nSubject:\s*$

That will match: 这将匹配:

  • ^From:.*? Match From: from the start of the string followed by any char non greedy Match From:从字符串的开头跟随任何char非贪婪
  • assistant@gmail\\.com Match email assistant@gmail\\.com匹配电子邮件
  • .* Match 0+ times any char except a newline .*匹配除了换行符之外的任何字符的0+次
  • (?:\\n.*) Match 0+ times a newline and the whole line until the end of the line (?:\\n.*)匹配换行符和整行的0+倍,直到行尾
  • \\nSubject:\\s*$ Match a newline, Subject, 0+ times a whitespace char and assert the end of the line \\nSubject:\\s*$匹配换行符,主题,空白字符的0+次并断言该行的结尾

Regen demo Regen演示

The other way around: 另一种方式:

^Subject:\s*(?:\n.*)*\nFrom:.*?assistant@gmail\.com

Regex demo 正则表达式演示

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

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