Advertisement
petur_stoqnov

Untitled

Aug 14th, 2021
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.         int rows = Integer.parseInt(sc.nextLine());
  12.  
  13.  
  14.         while (rows >= 0) {
  15.             String command = sc.nextLine().trim();
  16.  
  17.             Pattern pattern = Pattern.compile("([@*])([A-Z][a-z]{2,})(\\1)[:][ ](([\\[])([A-z]){1}([\\]])([|])){3}$");
  18.             Matcher matcher = pattern.matcher(command);
  19.             boolean matches = matcher.matches();
  20.  
  21.             if (matches) {
  22.                 String[] words = command.split(" ");
  23.                 String word = words[0].replaceAll("\\*", "");
  24.                 word = word.replaceAll("@", "");
  25.  
  26.                 String chars = words[1].replaceAll("\\|", " ");
  27.                 chars = chars.replaceAll("\\[", "");
  28.                 chars = chars.replaceAll("]", "");
  29.                 String[] charsWords = chars.split("\\s+");
  30.  
  31.                 StringBuilder result = new StringBuilder("");
  32.                 for (String charsWord : charsWords) {
  33.                     result.append((int) charsWord.charAt(0)).append(" ");
  34.                 }
  35.  
  36.                 System.out.printf("%s %s%n", word, result.toString().trim());
  37.             } else {
  38.                 System.out.println("Valid message not found!");
  39.             }
  40.  
  41.             rows--;
  42.         }
  43.  
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement