Valik98

Post Office

Nov 26th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package RegularExpressions_8.more_exercises;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class PostOffice {
  9.     public static void main(String[] args) {
  10.         Scanner console = new Scanner(System.in);
  11.  
  12.         String capitalLetters = "";
  13.         HashMap<String, Integer> capitalLetterAndLength = new HashMap<>();
  14.  
  15.         String[] input = console.nextLine().split("\\|");
  16.  
  17.         for (int i = 0; i < input.length; i++) {
  18.             String currentTextPart = input[i];
  19.  
  20.             if (i == 0) {
  21.                 Pattern regexCapital = Pattern.compile("([#$%*&]+)(?<letters>\\w+)(\\1)");
  22.                 Matcher matcher = regexCapital.matcher(currentTextPart);
  23.  
  24.                 while (matcher.find()) {
  25.                     String capitalLetter = matcher.group("letters");
  26.                     capitalLetters += capitalLetter;
  27.                 }
  28.             } else if (i == 1) {
  29.                 Pattern regexCapital = Pattern.compile("(?<ascii>\\d{2}):(?<length>\\d{2})");
  30.                 Matcher matcher = regexCapital.matcher(currentTextPart);
  31.  
  32.                 while (matcher.find()) {
  33.                     int asciiPosition = Integer.parseInt(matcher.group("ascii"));
  34.                     int wordLength = Integer.parseInt(matcher.group("length"));
  35.  
  36.                     for (int j = 0; j < capitalLetters.length(); j++) {
  37.                         String currentLetter = String.valueOf(capitalLetters.charAt(j));
  38.                         if (currentLetter.equals(String.valueOf((char) asciiPosition))) {
  39.                             capitalLetterAndLength.putIfAbsent(currentLetter, wordLength);
  40.                         }
  41.                     }
  42.  
  43.                 }
  44.             } else if (i == 2) {
  45.                 Pattern regexCapital = Pattern.compile("(?<=\\s|^)([A-Za-z]+[^\\w\\d\\s][A-Za-z]+)|([A-Za-z]+)(?=\\s|$)");
  46.                 Matcher matcher = regexCapital.matcher(currentTextPart);
  47.  
  48.                 while (matcher.find()) {
  49.                     String currentWord = matcher.group();
  50.  
  51.                     String firstLetter = String.valueOf(currentWord.charAt(0));
  52.                     int length = Integer.parseInt(String.valueOf(currentWord.substring(1).length()));
  53.  
  54.                     if (capitalLetterAndLength.containsKey(firstLetter)){
  55.                         if (capitalLetterAndLength.get(firstLetter).equals(length)) {
  56.                             System.out.println(currentWord);
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment