Advertisement
petur_stoqnov

Untitled

Aug 14th, 2021
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 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.             String[] input = command.split(" @");
  17.             if(input.length == 2){
  18.                 command = "@" + input[1];
  19.             }
  20.  
  21.             input = command.split(" *");
  22.             if(input.length == 2){
  23.                 command = "*" + input[1];
  24.             }
  25.  
  26.             Pattern pattern = Pattern.compile("([@*])([A-Z][a-z]{2,})(\\1)[:][ ](([\\[])([A-z]){1}([\\]])([|])){3}$");
  27.             Matcher matcher = pattern.matcher(command);
  28.             boolean matches = matcher.matches();
  29.  
  30.             if (matches) {
  31.                 String[] words = command.split(" ");
  32.                 String word = words[0].replaceAll("\\*", "");
  33.                 word = word.replaceAll("@", "");
  34.  
  35.                 String chars = words[1].replaceAll("\\|", " ");
  36.                 chars = chars.replaceAll("\\[", "");
  37.                 chars = chars.replaceAll("]", "");
  38.                 String[] charsWords = chars.split("\\s+");
  39.  
  40.                 StringBuilder result = new StringBuilder("");
  41.                 for (String charsWord : charsWords) {
  42.                     result.append((int) charsWord.charAt(0)).append(" ");
  43.                 }
  44.  
  45.                 System.out.printf("%s %s%n", word, result.toString().trim());
  46.             } else {
  47.                 System.out.println("Valid message not found!");
  48.             }
  49.  
  50.             rows--;
  51.         }
  52.  
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement