Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RegularExpressions_8.more_exercises;
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class PostOffice {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String capitalLetters = "";
- HashMap<String, Integer> capitalLetterAndLength = new HashMap<>();
- String[] input = console.nextLine().split("\\|");
- for (int i = 0; i < input.length; i++) {
- String currentTextPart = input[i];
- if (i == 0) {
- Pattern regexCapital = Pattern.compile("([#$%*&]+)(?<letters>\\w+)(\\1)");
- Matcher matcher = regexCapital.matcher(currentTextPart);
- while (matcher.find()) {
- String capitalLetter = matcher.group("letters");
- capitalLetters += capitalLetter;
- }
- } else if (i == 1) {
- Pattern regexCapital = Pattern.compile("(?<ascii>\\d{2}):(?<length>\\d{2})");
- Matcher matcher = regexCapital.matcher(currentTextPart);
- while (matcher.find()) {
- int asciiPosition = Integer.parseInt(matcher.group("ascii"));
- int wordLength = Integer.parseInt(matcher.group("length"));
- for (int j = 0; j < capitalLetters.length(); j++) {
- String currentLetter = String.valueOf(capitalLetters.charAt(j));
- if (currentLetter.equals(String.valueOf((char) asciiPosition))) {
- capitalLetterAndLength.putIfAbsent(currentLetter, wordLength);
- }
- }
- }
- } else if (i == 2) {
- Pattern regexCapital = Pattern.compile("(?<=\\s|^)([A-Za-z]+[^\\w\\d\\s][A-Za-z]+)|([A-Za-z]+)(?=\\s|$)");
- Matcher matcher = regexCapital.matcher(currentTextPart);
- while (matcher.find()) {
- String currentWord = matcher.group();
- String firstLetter = String.valueOf(currentWord.charAt(0));
- int length = Integer.parseInt(String.valueOf(currentWord.substring(1).length()));
- if (capitalLetterAndLength.containsKey(firstLetter)){
- if (capitalLetterAndLength.get(firstLetter).equals(length)) {
- System.out.println(currentWord);
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment