Advertisement
dimipan80

Java Regex: Query Mess

Aug 4th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. /*
  2.  * Ivancho participates in a team project with colleagues at SoftUni.
  3.  * They have to develop an application, but something mysterious happened – at the last moment all team members… disappeared!
  4.  * And guess what? He is left alone to finish the project.
  5.  * All that is left to do is to parse the input data and store it in a special way, but Ivancho has no idea how to do that! Can you help him?
  6.  *
  7.  * The Input comes from the console on a variable number of lines and ends when the keyword "END" is received.  
  8.  * For each row of the input, the query string contains pairs field=value.
  9.  * Within each pair, the field name and value are separated by an equals sign, '='.
  10.  * The series of pairs are separated by an ampersand, '&'.
  11.  * The question mark is used as a separator and is not part of the query string.
  12.  * A URL query string may contain another URL as value.
  13.  * SPACE is encoded as '+' or "%20". Letters (A-Z and a-z), numbers (0-9), the characters '*', '-', '.', '_'
  14.  *   and other non-special symbols are left as-is.
  15.  *
  16.  * Output
  17.  * For each input line, print on the console a line containing the processed string as follows:  
  18.  *      key=[value]nextkey=[another value] … etc.
  19.  * Multiple whitespace characters should be reduced to one inside value/key names,
  20.  *   but there shouldn’t be any whitespaces before/after extracted keys and values.
  21.  * If a key already exists, the value is added with comma and space after other values of the existing key in the current string.
  22.  */
  23.  
  24.  
  25. import java.io.BufferedReader;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.util.ArrayList;
  29. import java.util.LinkedHashMap;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32.  
  33. public class QueryMess {
  34.     public static void main(String[] args) throws IOException {
  35.  
  36.         BufferedReader reader =
  37.                 new BufferedReader(new InputStreamReader(System.in));
  38.  
  39.         String inputLine = reader.readLine();
  40.         while (!inputLine.equals("END")) {
  41.             extractValidKeyValuePairsAndPrintOnOutput(inputLine.split("[?&]"));
  42.             System.out.println();
  43.             inputLine = reader.readLine();
  44.         }
  45.  
  46.     }
  47.  
  48.     private static void extractValidKeyValuePairsAndPrintOnOutput(String[] queryPairs) {
  49.         LinkedHashMap<String, ArrayList<String>> keysValuesMap = new LinkedHashMap<>();
  50.  
  51.         Pattern pairsPattern = Pattern.compile("(\\S+)=(\\S+)");
  52.         Matcher pairMatcher;
  53.         ArrayList<String> keyValues;
  54.         for (String pair : queryPairs) {
  55.             pairMatcher = pairsPattern.matcher(pair);
  56.             if (pairMatcher.find()) {
  57.                 String key =
  58.                         removeSpaceCharactersFromKeyValuePairs(pairMatcher.group(1));
  59.  
  60.                 String value =
  61.                         removeSpaceCharactersFromKeyValuePairs(pairMatcher.group(2));
  62.  
  63.                 keyValues = new ArrayList<>();
  64.                 if (keysValuesMap.containsKey(key)) {
  65.                     keyValues = keysValuesMap.get(key);
  66.                 }
  67.  
  68.                 if (!keyValues.contains(value)) {
  69.                     keyValues.add(value);
  70.                 }
  71.  
  72.                 keysValuesMap.put(key, keyValues);
  73.             }
  74.         }
  75.  
  76.         for (String keyField : keysValuesMap.keySet()) {
  77.             System.out.printf("%s=", keyField);
  78.             System.out.print(keysValuesMap.get(keyField));
  79.         }
  80.     }
  81.  
  82.     private static String removeSpaceCharactersFromKeyValuePairs(String keyWord) {
  83.         StringBuilder resultWord = new StringBuilder();
  84.         String[] wordParts = keyWord.trim().split("(%20)+");
  85.         String[] smallParts;
  86.         StringBuilder sb;
  87.         for (String part : wordParts) {
  88.             smallParts = part.trim().split("\\++");
  89.             sb = new StringBuilder();
  90.             for (String str : smallParts) {
  91.                 sb.append(str).append(" ");
  92.             }
  93.  
  94.             resultWord.append(sb.toString().trim()).append(" ");
  95.         }
  96.  
  97.         return resultWord.toString().trim();
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement