The regex: /email.+?\b(\S+@\S+)/i
Working Example
in php:
preg_match_all("/email.+?\b(\S+@\S+)/i", $input_lines, $output_array);
$output_array[1]
will now contain your email addresses
I removed the m
flag - as this changes the way $
and ^
work which are not being used.
The breakdown of this is as follows:
email
this just matches the text email
- the i after the final /
makes it not care about case
.+?
will match any character other than new line one or more times, but matching as few characters as possible see Regex Laziness
\b
will match a word boundary - that is between a word character and a non word character - see Word boundaries
(
starts a capturing group - because this is the first one, this is why it is found in $output_array[1]
, if you had a second any matches would be in $output_array[2]
\S+
matches anything that isn't whitespace one or more times
@
matches the '@' character
\S+
matches anything that isn't whitespace one or more times
)
this closes the capturing group
We could start a huge debate over whether \S@\S
is the best way to match an email address - I think its good enough for this purpose, but if you want to go down the rabbit hole see here: Using a regular expression to validate an email address