Advertisement
Guest User

wiki Style Regex replacer

a guest
Oct 11th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4.  
  5. public class Test {
  6.  
  7.     private static String removeItalic(CharSequence sequence) {
  8.         Pattern patt = Pattern.compile("_\\*(.+?)\\*_");
  9.         Matcher m = patt.matcher(sequence);
  10.         StringBuffer sb = new StringBuffer(sequence.length());
  11.         while (m.find()) {
  12.             String text = m.group(1);
  13.             // ... possibly process 'text' ...
  14.             m.appendReplacement(sb, Matcher.quoteReplacement(text));
  15.         }
  16.         m.appendTail(sb);
  17.         return sb.toString();
  18.     }
  19.  
  20.     private static String removeBold(CharSequence sequence) {
  21.         Pattern patt = Pattern.compile("\\*(.+?)\\*");
  22.         Matcher m = patt.matcher(sequence);
  23.         StringBuffer sb = new StringBuffer(sequence.length());
  24.         while (m.find()) {
  25.             String text = m.group(1);
  26.             // ... possibly process 'text' ...
  27.             m.appendReplacement(sb, Matcher.quoteReplacement(text));
  28.         }
  29.         m.appendTail(sb);
  30.         return sb.toString();
  31.     }
  32.    
  33.    
  34.     private static String removeUrl(CharSequence sequence) {
  35.         Pattern patt = Pattern.compile("\\[(.+?)\\|\\]");
  36.         Matcher m = patt.matcher(sequence);
  37.         StringBuffer sb = new StringBuffer(sequence.length());
  38.         while (m.find()) {
  39.             String text = m.group(1);
  40.             // ... possibly process 'text' ...
  41.             m.appendReplacement(sb, Matcher.quoteReplacement(text));
  42.         }
  43.         m.appendTail(sb);
  44.         return sb.toString();
  45.     }
  46.    
  47.    
  48.     public static String cleanWikiFormat(CharSequence sequence) {
  49.         return Test.removeUrl(Test.removeBold(Test.removeItalic(sequence)));
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.         String text = "[hello|] this is just a *[test|]* to clean wiki *type* and _*formatting*_";
  54.         System.out.println("Original");
  55.         System.out.println(text);
  56.         text = Test.cleanWikiFormat(text);
  57.         System.out.println("CHANGED");
  58.         System.out.println(text);
  59.  
  60.  
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement