Advertisement
dimipan80

8. Extract E-mails

Sep 13th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 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: info@softuni-bulgaria.org, kiki@hotmail.co.uk,
  14.  * no-reply@github.com, s.peterson@mail.uu.net, info-bg@software-university.software.academy. */
  15.  
  16. import java.util.Scanner;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19.  
  20. public class _08_ExtractEmails {
  21.  
  22.     public static void main(String[] args) {
  23.         // TODO Auto-generated method stub
  24.         Scanner scan = new Scanner(System.in);
  25.         System.out.println("Enter your text on single line:");
  26.         String inputText = scan.nextLine();
  27.  
  28.         Pattern mailPattern = Pattern
  29.                 .compile("[A-Za-z]+[.-_]*[A-Za-z]+@[A-Za-z]+[-]*[.A-Za-z]+[A-Za-z]");
  30.  
  31.         Matcher matcher = mailPattern.matcher(inputText);
  32.         System.out
  33.                 .println("All extracted E-mail addresses from Given Text are:");
  34.         while (matcher.find()) {
  35.             System.out.println(matcher.group());
  36.         }
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement