kalinikov

03. Anonymous Vox

Nov 30th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class AnonymousVox {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String encodedText = scanner.nextLine();
  12. String placeholdersText = scanner.nextLine();
  13.  
  14. List<String> placeholders = new ArrayList<>();
  15.  
  16. while (placeholdersText.indexOf('{') >= 0) {
  17. int firstIndex = placeholdersText.indexOf('{');
  18. int lastIndex = placeholdersText.indexOf('}');
  19.  
  20. StringBuilder placeholder = new StringBuilder();
  21.  
  22. for (int i = firstIndex + 1; i < lastIndex; i++) {
  23. placeholder.append(placeholdersText.charAt(i));
  24. }
  25. placeholders.add(placeholder.toString());
  26. placeholdersText = placeholdersText.substring(lastIndex + 1);
  27. }
  28.  
  29. String regex = "(?<start>[A-Za-z]+)(?<placeholder>.+)(?<end>\\1)";
  30. Pattern pattern = Pattern.compile(regex);
  31. Matcher matcher = pattern.matcher(encodedText);
  32.  
  33. while (matcher.find() && !placeholders.isEmpty()) {
  34. String placeholder = matcher.group("placeholder");
  35. int firstIndex = encodedText.indexOf(placeholder);
  36. String encodedStart = encodedText.substring(0, firstIndex);
  37. String encodedEnd = encodedText.substring(firstIndex + placeholder.length());
  38.  
  39. encodedText = encodedStart
  40. + placeholders.get(0)
  41. + encodedEnd;
  42.  
  43. //encodedText = encodedText.replaceFirst(placeholder, placeholders.get(0));
  44. placeholders.remove(0);
  45.  
  46. }
  47.  
  48. System.out.println(encodedText);
  49.  
  50.  
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment