【问题标题】:java regex to clear mediawiki markup [duplicate]java正则表达式清除mediawiki标记[重复]
【发布时间】:2012-10-11 19:13:10
【问题描述】:

可能重复:
Wikipedia : Java library to remove wikipedia text markup removal

我必须清理一些来自 Confluence 的内容。该内容几乎是干净的;但是,有些事情是这样的:

  1. [链接|]:没有url部分的链接
  2. *[link|]*:粗体链接(不带 url 部分)
  3. *文本*:粗体文本
  4. _*文本*_:斜体粗体文本

等等。 我需要编写一个清除所有这些的正则表达式,所以,我做了类似的事情:

String wikiCleanMarkupRegex = "\\\\[(.*?)[\\\\|.*?]?\\\\]|\\\\*(.*?)\\\\*|_(.*?)_";

但这并不能清除所有内容,我的意思是,如果我将 #2 中的链接提供给它,我会得到:

[链接|]

这不是我想要的,我想得到“链接”……所以,我需要一次又一次地重新解析字符串,直到找不到其他匹配项。

这真的很慢,因为有数百万条记录需要清理,那么,有没有什么方法可以一次完成所有的正则表达式?

非常感谢。

【问题讨论】:

  • 另外,如果我有类似 _*[link|]*_: A link (without the url part) in bold and italic的东西,我需要解析它3次,一次删除斜体,其他删除粗体和最后一个删除括号......这对于我需要的东西来说太慢了

标签: java regex performance mediawiki matcher


【解决方案1】:

既然看起来基本上就是三种代码格式:italicboldLINK

我会做一个 3-pass 正则表达式替换器。

并且根据您给出的输入的优先顺序应该是:

/**
 * FIRST REMOVE ITALICS, THEN BOLD, THEN URL
 */
public static String cleanWikiFormat(CharSequence sequence) {
    return Test.removeUrl(Test.removeBold(Test.removeItalic(sequence)));
}

这是一个示例代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

    private static String removeItalic(CharSequence sequence) {
        Pattern patt = Pattern.compile("_\\*(.+?)\\*_");
        Matcher m = patt.matcher(sequence);
        StringBuffer sb = new StringBuffer(sequence.length());
        while (m.find()) {
            String text = m.group(1);
            // ... possibly process 'text' ...
            m.appendReplacement(sb, Matcher.quoteReplacement(text));
        }
        m.appendTail(sb);
        return sb.toString();
    }

    private static String removeBold(CharSequence sequence) {
        Pattern patt = Pattern.compile("\\*(.+?)\\*");
        Matcher m = patt.matcher(sequence);
        StringBuffer sb = new StringBuffer(sequence.length());
        while (m.find()) {
            String text = m.group(1);
            // ... possibly process 'text' ...
            m.appendReplacement(sb, Matcher.quoteReplacement(text));
        }
        m.appendTail(sb);
        return sb.toString();
    }


    private static String removeUrl(CharSequence sequence) {
        Pattern patt = Pattern.compile("\\[(.+?)\\|\\]");
        Matcher m = patt.matcher(sequence);
        StringBuffer sb = new StringBuffer(sequence.length());
        while (m.find()) {
            String text = m.group(1);
            // ... possibly process 'text' ...
            m.appendReplacement(sb, Matcher.quoteReplacement(text));
        }
        m.appendTail(sb);
        return sb.toString();
    }


    public static String cleanWikiFormat(CharSequence sequence) {
        return Test.removeUrl(Test.removeBold(Test.removeItalic(sequence)));
    }

    public static void main(String[] args) {
        String text = "[hello|] this is just a *[test|]* to clean wiki *type* and _*formatting*_";
        System.out.println("Original");
        System.out.println(text);
        text = Test.cleanWikiFormat(text);
        System.out.println("CHANGED");
        System.out.println(text);
    }
}

以下将给出:

Original
[hello|] this is just a *[test|]* to clean wiki *type* and _*formatting*_
CHANGED
hello this is just a test to clean wiki type and formatting

【讨论】:

    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多