Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RegularExpressionsMoreExercises;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class PostOfficeExternal {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] message = scanner.nextLine().split("\\|");
- String firstPart = message[0];
- String secondPart = message[1];
- String thirdPart = message[2];
- String regexFirst = "([#$%*&])(?<capitalLetter>[A-Z]+)\\1";
- Pattern pattern = Pattern.compile(regexFirst);
- Matcher matcher = pattern.matcher(firstPart);
- LinkedHashMap<Character, Integer> lettersLength = new LinkedHashMap<>();
- if (matcher.find()) {
- for (char letter : matcher.group("capitalLetter").toCharArray()) {
- lettersLength.put(letter, 0);
- }
- }
- String regexSecond = "(?<asciiCode>[0-9]{2}):(?<length>[0-9]{2})";
- Pattern patternSecond = Pattern.compile(regexSecond);
- Matcher matcherSecond = patternSecond.matcher(secondPart);
- while (matcherSecond.find()) {
- for (char letter : matcher.group("capitalLetter").toCharArray()) {
- if (letter == (char) Integer.parseInt(matcherSecond.group("asciiCode"))) {
- lettersLength.put(letter, Integer.parseInt(matcherSecond.group("length")));
- break;
- }
- }
- }
- String[] words = thirdPart.split(" ");
- for (Map.Entry<Character, Integer> chars : lettersLength.entrySet()) {
- char currentChar = chars.getKey();
- int currentLength = chars.getValue() + 1;
- for (int j = 0; j < words.length; j++) {
- String currentWord = words[j];
- if (currentWord.toCharArray()[0] == currentChar && currentLength == currentWord.length()) {
- System.out.println(currentWord);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement