veronikaaa86

Fucking Shit Regex

Feb 17th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package Regex;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Lab06_ReplaceTag {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         String regex = "(^|(?<=\\s))(?<replacedOne><a+) (.+?\\w\\\")(?<replacedTwo>\\>)(\\w+)(?<replacedThree>\\<\\/a\\>)";
  14.         Pattern pattern = Pattern.compile(regex);
  15.  
  16.         while (true) {
  17.             String input = reader.readLine();
  18.             if ("end".equals(input)) break;
  19.  
  20.             Matcher matcher = pattern.matcher(input);
  21.  
  22.             if (!matcher.find()) {
  23.                 System.out.println(input);
  24.             } else if (matcher.find()){
  25.                 String matchStr = "";
  26.                 while (matcher.find()) {
  27.                     matchStr = matcher.group();
  28.                     matchStr = matchStr.replaceAll("<a", "[URL");
  29.                     matchStr = matchStr.replaceAll(">", "]");
  30.                     matchStr = matchStr.replaceAll("</a", "[/URL");
  31.  
  32.                     System.out.println(matchStr);
  33.                 }
  34.             }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment