简体   繁体   中英

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:

/^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/

May be you get some idea from this.

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

Demo:: 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.

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
  • assistant@gmail\\.com Match email
  • .* Match 0+ times any char except a newline
  • (?:\\n.*) Match 0+ times a newline and the whole line until the end of the line
  • \\nSubject:\\s*$ Match a newline, Subject, 0+ times a whitespace char and assert the end of the line

Regen demo

The other way around:

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

Regex demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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