-4

I'd like to extract the email address frim this string:

ezeze smtp.mailfrom=mymail@gmail.com; rerer

How should I write my regex in preg_match to find the email?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user4546765
  • 65
  • 1
  • 6

2 Answers2

0

You can try this regex pattern:

(?<=mailfrom=)[^;]+(?=;)

(?<=mailfrom=) // assert 'mailfrom=' on the left
[^;]+          // 1 or more characters not ';'
(?=;)          // assert ';' on the right

Demo

linden2015
  • 887
  • 7
  • 9
0

There is no need for any capture groups or lookarounds (which will slow down the regex engine).

Find/Match the starting marker substring, then "forget" those characters with \K, then match all non-semicolon characters in a greedy fashion.

Code: (Demo)

$string = 'ezeze smtp.mailfrom=mymail@gmail.com; rerer';

echo preg_match(
         '/smtp\.mailfrom=\K[^;]+/',
         $string,
         $match
     )
     ? $match[0]
     : 'no email address';

Output:

mymail@gmail.com
mickmackusa
  • 43,625
  • 12
  • 83
  • 136