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

use*_*991 2 html java regex

我正在处理包含图像标记和新行标记的文本段落.目标是通过将所有单词charachter的颜色更改为白色来使所有非单词charechter显示清楚.我正在使用java作为编程语言.我想使用正则表达式,但问题它改变了图像标签内的单词charechters.

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();
}
Run Code Online (Sandbox Code Playgroud)

所需结果的例子.输入: 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>"). 
Run Code Online (Sandbox Code Playgroud)

Ste*_*han 8

NOTA:

我希望这次讨论能够为休闲读者和/或Google员工提供帮助,并将成为Regex与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);
     }
}
Run Code Online (Sandbox Code Playgroud)

产量

<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>").
Run Code Online (Sandbox Code Playgroud)

解决方案#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());
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

<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>
Run Code Online (Sandbox Code Playgroud)

讨论

让我们比较两种方法:

数量上

除导入外,两个代码共享相同的代码行数.排除JDK提供的核心类和封面下实例化的类,解决方案#2需要3个额外的类(Jsoup,ElementTextNode),而解决方案#1需要2(Matcher,Pattern).解决方案#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
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,战斗结束了平局.

结论

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