Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int rows = Integer.parseInt(sc.nextLine());
- while (rows >= 0) {
- String command = sc.nextLine().trim();
- String[] input = command.split(" @");
- if(input.length == 2){
- command = "@" + input[1];
- }
- input = command.split(" *");
- if(input.length == 2){
- command = "*" + input[1];
- }
- Pattern pattern = Pattern.compile("([@*])([A-Z][a-z]{2,})(\\1)[:][ ](([\\[])([A-z]){1}([\\]])([|])){3}$");
- Matcher matcher = pattern.matcher(command);
- boolean matches = matcher.matches();
- if (matches) {
- String[] words = command.split(" ");
- String word = words[0].replaceAll("\\*", "");
- word = word.replaceAll("@", "");
- String chars = words[1].replaceAll("\\|", " ");
- chars = chars.replaceAll("\\[", "");
- chars = chars.replaceAll("]", "");
- String[] charsWords = chars.split("\\s+");
- StringBuilder result = new StringBuilder("");
- for (String charsWord : charsWords) {
- result.append((int) charsWord.charAt(0)).append(" ");
- }
- System.out.printf("%s %s%n", word, result.toString().trim());
- } else {
- System.out.println("Valid message not found!");
- }
- rows--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement