Advertisement
SPDG57

The Isle of Man TT Race - Regex, named groups, quantifiers

Dec 3rd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 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.     public static String encrypt(String racer, String geohash, int key){
  21.         String decryptedGeohash = "";
  22.  
  23.         StringBuilder builder = new StringBuilder();
  24.         for (int i = 0; i < geohash.length(); i++) {
  25.             char character = geohash.charAt(i);
  26.  
  27.             int encryptedCharacter = (int) character + key;
  28.  
  29.             builder.append((char) encryptedCharacter);
  30.         }
  31.         decryptedGeohash = builder.toString();
  32.  
  33.         return racer + " -> " + decryptedGeohash;
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.         Scanner scanner = new Scanner(System.in);
  38.  
  39.         final String REGEX =  "^([\\#|\\$|\\%|\\*|\\&])(?<racer>[A-Za-z]+)\\1\\=(?<length>[0-9]+)\\!\\!(?<geohash>.+)$";
  40.         Pattern pattern = Pattern.compile(REGEX);
  41.        
  42.         while(true){
  43.             String input = scanner.nextLine();
  44.             Matcher matcher = pattern.matcher(input);
  45.             if(matcher.find()){
  46.                 String racer = matcher.group("racer");
  47.                 String message = matcher.group("geohash");
  48.                 int length = Integer.parseInt(matcher.group("length"));
  49.                 if(message.length() == length){
  50.                     System.out.print("Coordinates found! ");
  51.                     System.out.println(encrypt(racer, message, length));
  52.                     return;
  53.                 }
  54.             }
  55.                 System.out.println("Nothing found!");
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement