【问题标题】:Adding tags to every word except non characters outside angle brackets为除尖括号外的非字符外的每个单词添加标签
【发布时间】:2014-03-10 13:44:12
【问题描述】:

我正在处理包含图像标签和新行标签的文本段落。目标是通过将所有单词字符的颜色更改为白色来清楚地显示所有非单词字符。 我使用java作为编程语言。 我正在尝试使用正则表达式,但 问题它会更改图像标签内的单词字符。

String RegEx = "\\w|[àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ]";

try {
    Pattern mypattern = Pattern.compile(RegEx, Pattern.CASE_INSENSITIVE);
    Matcher myMatcher = mypattern.matcher(sentence);
    int offset = 0;
    while (myMatcher.find()) {
        int start = myMatcher.start() + offset;
        int end = myMatcher.end() + offset;
        sentence = sentence.substring(0, start) + "<font color=\"white\">" + sentence.substring(start, end) + "</font>" + sentence.substring(end, sentence.length());
        offset += 28;
    }
} catch (Exception e) {
    e.printStackTrace();
}

所需结果的示例。 输入: Most implementations&lt;img title="hello:" alt="hello:{}" src="http://images.doctissimo.fr/hello.gif" class="wysiwyg_smiley" /&gt; provide ASDF as a module, and you can simply (require "asdf").

输出:

<font color="white">Most<font> <font color="white">implementations<font><img title="hello:" alt="hello:{}" src="http://images.doctissimo.fr/hello.gif" class="wysiwyg_smiley" /> <font color="white">provide<font> <font color="white">ASDF<font> <font color="white">as<font> <font color="white">a<font> <font color="white">module<font>, <font color="white">and<font> <font color="white">you<font> <font color="white">can<font> <font color="white">simply<font> (<font color="white">require<font> "<font color="white">asdf<font>"). 

【问题讨论】:

  • 使用 HTML 解析器/生成器!
  • 您无法使用 RegEx 解析 HTML。每次你尝试时,一个无所不能的抽象和独立于宗教的实体都会杀死一只小猫。
  • 你能看看JSoup吗?它可能会帮助你。 jsoup.org
  • @Kroltan 太好了,你让我很开心。 ;)
  • 看看这个解决方案:stackoverflow.com/a/21731443/363573

标签: java html regex


【解决方案1】:

注意:

我希望本次讨论对普通读者和/或 googler 将成为 Regex 与 HTML 之战中的“和平之窗” 解析器


解决方案 #1:使用正则表达式

示例代码

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

public class HelloWorld {
    public static void main(String []args){
        String sentence = "Most implementations<img title=\"hello:\" alt=\"hello:{}\" src=\"http://images.doctissimo.fr/hello.gif\" class=\"wysiwyg_smiley\" /> provide ASDF as a module, and you can simply (require \"asdf\").";
        String RegEx = "(?is)(\\w+|[\u00E0\u00C0\u00E2\u00C2\u00E4\u00C4\u00E1\u00C1\u00E9\u00C9\u00E8\u00C8\u00EA\u00CA\u00EB\u00CB\u00EC\u00CC\u00EE\u00CE\u00EF\u00CF\u00F2\u00D2\u00F4\u00D4\u00F6\u00D6\u00F9\u00D9\u00FB\u00DB\u00FC\u00DC\u00E7\u00C7\u2019\u00F1]+)(<[^>]+>)?";

        Pattern mypattern = Pattern.compile(RegEx);

        Matcher myMatcher = mypattern.matcher(sentence);
        String output=myMatcher.replaceAll("<font color=\"white\">$1</font>$2");

        System.out.println(output);
     }
}

输出

<font color="white">Most</font> <font color="white">implementations</font> <img title="hello:" alt="hello:{}" src="http://images.doctissimo.fr/hello.gif" class="wysiwyg_smiley" /> <font color="white">provide</font> <font color="white">ASDF</font> <font color="white">as</font> <font color="white">a</font> <font color="white">module</font>, <font color="white">and</font> <font color="white">you</font> <font color="white">can</font> <font color="white">simply</font> (<font color="white">require</font> "<font color="white">asdf</font>").

解决方案 #2:使用 Jsoup

示例代码

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;

public class HelloWorldWithJsoup {
    public static void main(String[] args) {
        String sentence = "Most implementations<img title=\"hello:\" alt=\"hello:{}\" src=\"http://images.doctissimo.fr/hello.gif\" class=\"wysiwyg_smiley\" /> provide ASDF as a module, and you can simply (require \"asdf\").";

        Element body = Jsoup.parse(sentence).body();

        for (TextNode textNode : body.textNodes()) {
            textNode.wrap("<font color=\"white\"></font>");
        }

        System.out.println(body.html());
    }
}

输出

<font color="white">Most implementations</font>
<img title="hello:" alt="hello:{}" src="http://images.doctissimo.fr/hello.gif" class="wysiwyg_smiley" />
<font color="white"> provide ASDF as a module, and you can simply (require &quot;asdf&quot;).</font>

讨论

让我们比较两种方法:

定量

除了导入之外,两个代码共享相同的代码行数。排除 JDK 提供的核心类和在封面下实例化的类,解决方案#2 需要 3 个额外的类(JsoupElementTextNode),而解决方案#1 需要 2 个(MatcherPattern) .解决方案#2 要求您在代码中添加一个依赖项,而解决方案#1 已准备好使用 JDK。

定性

从可读性的角度来看,它们都是直截了当的。但是,对于没有经验的 Java regex API 阅读器来说,理解代码可能具有挑战性。从可维护性的角度来看,这里使用的正则表达式很长,您需要 unicode 功能。 Jsoup 解决方案仅依赖于有据可查的方法。最后,Jsoup 产生的输出更加尊重 HTML 的良好实践。较少使用font 标签。

比较矩阵

Quantitatively:     |  Regex vs Jsoup
--------------------------------------
Lines of code       |    O        O
Classes used        |    O        X
Dependency required |    O        X


Qualitatively:      |  Regex vs Jsoup
--------------------------------------
Readability         |    O        O
Maintenability      |    X        O
HTML good practices |    X        O

如您所见,战斗以平局告终。

结论

IMO,在这个用例中,在一种解决方案或另一种解决方案之间进行选择将在很大程度上取决于每个解决方案的产生结果和预期结果。 Jsoup 解决方案以白色绘制像 ,) 这样的字符。正则表达式方法没有。对于最终用户,需要哪种输出将导致一种或另一种解决方案。

【讨论】:

  • @user3292991 你选择了哪种解决方案?
  • 我选择了正则表达式解决方案,因为我的工作目标是澄清非文字承租人。
  • 这个正则表达式在什么方面是可读的?你记住了所有这些 un​​icode 代码点吗?
猜你喜欢
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 2016-11-20
  • 2018-11-28
  • 1970-01-01
相关资源
最近更新 更多