Write a program to extract all email addresses from a given text. The text comes at the only input line. Print the emails on the console, each at a separate line. Emails are considered to be in format <user>@<host>, where:
<user> is a sequence of letters and digits, where '.', '-' and '_' can appear between them.
<host> is a sequence of at least two words, separated by dots '.'. Each word is sequence of letters and can have hyphens '-' between the letters.
Examples of valid emails: firstname.lastname@example.org, email@example.com, firstname.lastname@example.org, email@example.com, firstname.lastname@example.org.
Here is my solution:
$str = readline();
$pattern = '/(^|\s)[a-zA-Z0-9]+[\w\-\.]*@[a-zA-Z-]+(\.[a-zA-Z]+)+/';
preg_match_all($pattern, $str, $matches);
$result = implode("\n", $matches);
I am using Regex101
...and a cheatsheet: