Advertisement
SensaBG

Untitled

Mar 30th, 2024
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package RegularExpressionsMoreExercises;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class PostOfficeExternal {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String[] message = scanner.nextLine().split("\\|");
  12.         String firstPart = message[0];
  13.         String secondPart = message[1];
  14.         String thirdPart = message[2];
  15.  
  16.         String regexFirst = "([#$%*&])(?<capitalLetter>[A-Z]+)\\1";
  17.         Pattern pattern = Pattern.compile(regexFirst);
  18.         Matcher matcher = pattern.matcher(firstPart);
  19.         LinkedHashMap<Character, Integer> lettersLength = new LinkedHashMap<>();
  20.  
  21.         if (matcher.find()) {
  22.             for (char letter : matcher.group("capitalLetter").toCharArray()) {
  23.                 lettersLength.put(letter, 0);
  24.             }
  25.         }
  26.  
  27.         String regexSecond = "(?<asciiCode>[0-9]{2}):(?<length>[0-9]{2})";
  28.         Pattern patternSecond = Pattern.compile(regexSecond);
  29.         Matcher matcherSecond = patternSecond.matcher(secondPart);
  30.  
  31.         while (matcherSecond.find()) {
  32.             for (char letter : matcher.group("capitalLetter").toCharArray()) {
  33.                 if (letter == (char) Integer.parseInt(matcherSecond.group("asciiCode"))) {
  34.                     lettersLength.put(letter, Integer.parseInt(matcherSecond.group("length")));
  35.                     break;
  36.  
  37.                 }
  38.             }
  39.         }
  40.  
  41.         String[] words = thirdPart.split(" ");
  42.         for (Map.Entry<Character, Integer> chars : lettersLength.entrySet()) {
  43.             char currentChar = chars.getKey();
  44.             int currentLength = chars.getValue() + 1;
  45.             for (int j = 0; j < words.length; j++) {
  46.                 String currentWord = words[j];
  47.                 if (currentWord.toCharArray()[0] == currentChar && currentLength == currentWord.length()) {
  48.                     System.out.println(currentWord);
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement