PHP正则表达式 - 在两个字符之间找到(保留最后一个字符)

Trying to help grab unique emails for a contest and have a few people using the gmail + symbol trick to get around it. I'm trying to get a clean way to filter the email so it removes everything in between the + and @ symbol so that:

tester+dn2934u2@gmail.com

becomes:

tester@gmail.com

So far I have:

\+(.*?)\@

which removes the + and @ symbols. Is there a way (and I'm sure there is) to tell regex to keep the @ character so I don't need to re-add it, replace it, or some other trick?

http://regex101.com/r/zM8sT3

You can use this:

\+[^@]+(?=@)

(?=@) is a lookahead assertion (its only a check) and means followed by @

You can instead use a negated class:

\+[^@]*

regex101 demo

Put it within your selector?

eg \+(.*?\@)

Just include the @ into the capture group:

\+(.*?@)

You can use this to match everything but the @ symbol:

\+[^@]+

Full php code:

$mail = preg_replace('%\+[^@]+%', '', $mail);
$email_stripped = preg_replace("/^(.+?)(\+.+)?(@.+)$/", "$1$3", $email);