java - 为除尖括号外的非字符外的每个单词添加标签

标签 java html regex

我正在处理包含图像标签和换行标签的文本段落。目标是通过将所有单词字符的颜色更改为白色,使所有非单词字符都清晰显示。 我正在使用 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<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").

输出:

<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>"). 

最佳答案

备注:

I hope this discussion will be an help for the casual reader and/or googler and will be "a window of peace" in the war of Regex vs HTML Parser.


解决方案 #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)而 Solution#1 需要 2(MatcherPattern)。解决方案#2 要求您在代码中加入依赖项,而解决方案#1 已准备好使用 JDK。

定性

从可读性的角度来看,它们都是直截了当的。然而,对于没有经验的 Java 正则表达式 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 解决方案以白色绘制 ) 等字符。正则表达式方法没有。对于最终用户,所需的输出将导致一种或另一种解决方案。

关于java - 为除尖括号外的非字符外的每个单词添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21724440/

相关文章:

javascript - 为什么 Chrome 显示的听众数量比实际数量少?

java - 我如何/我可以从 Eclipse 升级 Java?

java - 无法在启动时启动服务

java - 如何在Android中编辑二进制文件?

java - 将多个值从 Servlet 传递到 JSP,但我在 JSP 中只得到 1 个值

html - Angular - 在另一个组件的输入文本框中设置值

javascript - 使图像闪烁并自动调整大小以适应浏览器

具有递归支持的 Java 正则表达式库

python - 在 Python 中匹配 Unicode 字边界

正则表达式 PCRE,带有 a、b 的字母表,应匹配没有连续字符的单词