Brian Smith
Brian Smith

Reputation: 1481

Simple Email Regex Issue

I'm trying to combine a couple simple email PCRE regex checks for Postfix. I'll be honest, this is not my area of expertise.

Is there a way to combine the 2? or should they be left separate?

I know I need the < > to be optional, but adding ? to them did nothing.

This handles "<[email protected]>, <[email protected]>" format
(?:To|Cc):(?:.+?<.+?[@].+?>){2,}

This handles "[email protected], [email protected]" format
(?:To|Cc):(?:.+?[@].+?),.{2,}

Any help would be greatly appreciated.

Upvotes: 1

Views: 61

Answers (1)

anubhava
anubhava

Reputation: 785128

You can use this regex with optional < and > on either side of email address:

(?:To|Cc):(?:\h*<?\w[^@]*@\S+>?,?){2,}

This will match both of these inputs:

To: <[email protected]>, <[email protected]>
Cc: [email protected], [email protected]

RegEx Demo

Upvotes: 2

Related Questions