Advertisement
dimipan80

Java Regex: Phone Numbers

Aug 3rd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. /*
  2.  * You are given a string, holding ASCII characters.
  3.  * Find all name -> phone number pairs, format them and print them in an ordered list as list items.
  4.  * The name should be at least one letter long, can contain only letters and should always start with an uppercase letter.
  5.  * The phone number should be at least two digits long, should start and end with a digit (optionally, there might be a “+” in front)
  6.  *  and might contain the following symbols in it: “(“, “)”, “/”, “.”, “-”, “ “ (left and right bracket, slash, dot, dash and whitespace).
  7.  * Between a name and the phone number there might be any number of symbols, other than letters and “+”.
  8.  * Between the name -> phone number pairs there might be any number of ASCII symbols.
  9.  * The output format should be <b>[name]:</b> [phone number] (there is one space between the name and the phone number).
  10.  * Clear any characters other than digits and “+” from the number when printing the output.
  11.  * The Input will be read from the console. It will consist of several lines holding the input string.
  12.  * The command "END" denotes the end of input.
  13.  * The Output should hold the resulting Ordered List (on a Single line), or a Single Paragraph, holding “No matches!”
  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 PhoneNumbers {
  23.     public static void main(String[] args) {
  24.         StringBuilder inputText = new StringBuilder();
  25.  
  26.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
  27.             String inputLine = reader.readLine();
  28.             while (!inputLine.equals("END")) {
  29.                 inputText.append(inputLine);
  30.  
  31.                 inputLine = reader.readLine();
  32.             }
  33.  
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         }
  37.  
  38.         Pattern pattern = Pattern.compile("([A-Z][a-zA-Z]*)[^+a-zA-Z]*?(\\+?\\d[-./()0-9 ]*\\d)");
  39.         Matcher matcher = pattern.matcher(inputText.toString());
  40.  
  41.         boolean isMatch = matcher.find();
  42.         if (isMatch) {
  43.             System.out.print("<ol>");
  44.             while (isMatch) {
  45.                 System.out.printf("<li><b>%s:</b> %s</li>",
  46.                         matcher.group(1), matcher.group(2).replaceAll("[^+\\d]+", ""));
  47.  
  48.                 isMatch = matcher.find();
  49.             }
  50.  
  51.             System.out.print("</ol>");
  52.         } else {
  53.             System.out.println("<p>No matches!</p>");
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement