3
19/10/2024 3:07 am
Début du sujet
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.
- Examples of valid users: "stephan", "mike03", "s.johnson", "st_steward", "softuni-bulgaria", "12345".
- Examples of invalid users: ''--123", ".....", "nakov_-", "_steve", ".info".
<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 hosts: "softuni.bg", "software-university.com", "intoprogramming.info", "mail.softuni.org".
- Examples of invalid hosts: "helloworld", ".unknown.soft.", "invalid-host-", "invalid-".
-
Examples of valid emails: info@softuni-bulgaria.org, kiki@hotmail.co.uk, no-reply@github.com, s.peterson@mail.uu.net, info-bg@software-university.software.academy.
- Examples of invalid emails: --123@gmail.com, …@mail.bg, .info@info.info, _steve@yahoo.cn, mike@helloworld, mike@.unknown.soft., s.johnson@invalid-.
Examples:
1 Réponse
2
19/10/2024 3:10 am
Here is my solution:
<?php $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[0]); echo trim($result);
I am using Regex101
...and a cheatsheet: