Advertisement
dimipan80

Java Regex: Extract Emails

Aug 2nd, 2017
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. /* Write a program to extract all email addresses from given text.
  2.  * The text comes at the first input line.
  3.  * Print the emails in the output, each at a separate line.
  4.  * Emails are considered to be in format <user>@<host>, where:
  5.  * <user> is a sequence of letters and digits, where '.', '-' and '_' can appear between them.
  6.  * Examples of valid users: "stephan", "mike03", "s.johnson", "st_steward", "softuni-bulgaria",
  7.  * "12345". Examples of invalid users: ''--123", ".....", "nakov_-", "_steve", ".info".
  8.  * <host> is a sequence of at least two words, separated by dots '.'.
  9.  * Each word is sequence of letters and can have hyphens '-' between the letters.
  10.  * Examples of hosts: "softuni.bg", "software-university.com", "intoprogramming.info",
  11.  * "mail.softuni.org". Examples of invalid hosts: "helloworld", ".unknown.soft.",
  12.  * "invalid-host-", "invalid-".
  13.  * Example of valid emails: [email protected], [email protected],
  14.  
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. import java.util.regex.Matcher;
  20. import java.util.regex.Pattern;
  21.  
  22. public class ExtractEmails {
  23.     public static void main(String[] args) {
  24.         StringBuilder text = new StringBuilder();
  25.  
  26.         try (BufferedReader reader =
  27.                      new BufferedReader(new InputStreamReader(System.in))) {
  28.  
  29.             String line = reader.readLine();
  30.             while (!line.equals("end")) {
  31.                 text.append(line);
  32.  
  33.                 line = reader.readLine();
  34.             }
  35.  
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         Pattern pattern = Pattern
  41.                 .compile("(^|\\s)[a-z0-9][-._a-z0-9]*[a-z0-9]@[a-z0-9][-.a-z0-9]*[a-z0-9]\\.[a-z]{2,}");
  42.  
  43.         Matcher matcher = pattern.matcher(text);
  44.         while (matcher.find()) {
  45.             System.out.println(matcher.group().trim());
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement