Search code examples
phpregexpreg-matchemail-address

Find email address between specific substring and semicolon


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

ezeze [email protected]; rerer

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


Solution

  • You can try this regex pattern:

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

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

    Demo