Advertisement
SPDG57

Message Decrypter - Groups, quantifiers, int to char to str

Dec 4th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javafx.util.Pair;
  4.  
  5. import javax.xml.crypto.dsig.keyinfo.KeyValue;
  6. import java.lang.reflect.Array;
  7. import java.security.KeyStore;
  8. import java.text.Collator;
  9. import java.text.DecimalFormat;
  10. import java.util.*;
  11. import java.util.function.Predicate;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import java.util.stream.Collector;
  15. import java.util.stream.Collectors;
  16.  
  17.  
  18. public class Main {
  19.  
  20.  
  21.  
  22. public static void main(String[] args) {
  23. Scanner scanner = new Scanner(System.in);
  24.  
  25. final String REGEX = "^(\\$|\\%)(?<tag>[A-Z][a-z]{3,})\\1: \\[(?<first>[0-9]+)\\]\\|\\[(?<second>[0-9]+)\\]\\|\\[(?<third>[0-9]+)\\]\\|$";
  26. Pattern pattern = Pattern.compile(REGEX);
  27.  
  28. int n = Integer.parseInt(scanner.nextLine());
  29.  
  30. for (int i = 0; i < n; i++) {
  31. String input = scanner.nextLine();
  32. Matcher matcher = pattern.matcher(input);
  33.  
  34. if (matcher.find()) {
  35. String tag = matcher.group("tag");
  36. int first = Integer.parseInt(matcher.group("first"));
  37. int second = Integer.parseInt(matcher.group("second"));
  38. int third = Integer.parseInt(matcher.group("third"));
  39.  
  40. String firstDecrypted = (char) first + "";
  41. String secondDecrypted = (char) second + "";
  42. String thirdDecrypted = (char) third + "";
  43.  
  44. System.out.println(tag + ": " + firstDecrypted + secondDecrypted + thirdDecrypted);
  45. }else{
  46. System.out.println("Valid message not found!");
  47. }
  48. }
  49.  
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement