Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to extract all email addresses from given text.
- * The text comes at the first input line.
- * Print the emails in the output, 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-".
- * Example 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. */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class ExtractEmails {
- public static void main(String[] args) {
- StringBuilder text = new StringBuilder();
- try (BufferedReader reader =
- new BufferedReader(new InputStreamReader(System.in))) {
- String line = reader.readLine();
- while (!line.equals("end")) {
- text.append(line);
- line = reader.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- Pattern pattern = Pattern
- .compile("(^|\\s)[a-z0-9][-._a-z0-9]*[a-z0-9]@[a-z0-9][-.a-z0-9]*[a-z0-9]\\.[a-z]{2,}");
- Matcher matcher = pattern.matcher(text);
- while (matcher.find()) {
- System.out.println(matcher.group().trim());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement