[英]Adding tags to every word except non characters outside angle brackets
我正在處理包含圖像標記和新行標記的文本段落。 目標是通過將所有單詞charachter的顏色更改為白色來使所有單詞非常清晰地顯示出來。 我正在使用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();
}
所需結果的例子。 輸入: 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>").
NOTA:
我希望這次討論能夠為休閑讀者和/或Google員工提供幫助,並將成為Regex與HTML Parser戰爭中的“和平之窗”。
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>").
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 "asdf").</font>
讓我們比較兩種方法:
除導入外,兩個代碼共享相同的代碼行數。 排除JDK提供的核心類和封面下實例化的類,解決方案#2需要3個額外的類( Jsoup
, Element
和TextNode
),而解決方案#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
正如你所看到的,戰斗結束了平局 。
IMO,在這個用例中,在一個或另一個解決方案之間進行選擇將在很大程度上取決於每個解決方案的生成結果和預期結果。 該Jsoup溶液繪制字符等,
或)
為白色。 正則表達式方法沒有。 對於最終用戶,期望哪個輸出將導致一個或另一個解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.