2010-12-21 58 views
2

以下輸出由第三方工具生成,該工具根據XML模式驗證XML文件。它會輸出驗證錯誤。好的,這只是一個小小的背景。使用Perl編輯HTML文件

從上面的錯誤顯示在HTML中,我希望能夠使用Perl執行「語法突出顯示」。例如,我希望能夠突出上述文本的某些部分。

具體而言,我想將符合「Line [0-9] *」的任何文本着色爲粗體和紅色。我試圖嘗試正則表達式搜索和替換,但我沒有那麼成功。

任何指針/提示將是太棒了。

謝謝!

Line 8: Element 'alphabet', attribute 'letters': The value 'XYZ' does not match the fixed value constraint 'ABC'. 
Line 185: Element 'drink': The attribute 'coffee' is required but missing. 
Line 254: Element 'timeout': This element is not expected. 
Line 269: Element 'commands': This element is not expected. Expected is one of (eat, drink, sleep). 
Line 812: Element 'software': The attribute 'version' is required but missing. 
Line 876: Element 'windows-software': The attribute 'version' is required but missing. 
Line 890: Element 'contact': The attribute 'telephone' is required but missing. 
Line 890: Element 'operating': The attribute 'mode' is required but missing. 
+1

你或許應該表現出正則表達式,以及一些HTML代碼中要突出 – 2010-12-21 12:42:13

+0

上面的文字是什麼顯示在一個HTML文件。這就是我想要在 – sandster 2010-12-21 12:44:22

+0

上執行突出顯示你確實知道那裏沒有HTML,對吧?文本是否在'pre'塊中? – 2010-12-21 12:52:31

回答

1

有這樣的嘗試:

#!/usr/bin/perl 
use strict; 
use warnings; 

while(<DATA>) { 
    s#(Line \d+)#<span style="font-weight:bold;color:red;">$1</span>#; 
    s#(Element\s|attribute\s)('[^']*')#$1<span style="font-weight:bold;color:blue;">$2</span>#g; 
    print 
} 

__DATA__ 
Line 8: Element 'alphabet', attribute 'letters': The value 'XYZ' does not match the fixed value constraint 'ABC'. 
Line 185: Element 'drink': The attribute 'coffee' is required but missing. 
Line 254: Element 'timeout': This element is not expected. 

輸出

<span style="font-weight:bold;color:red;">Line 8</span>: Element <span style="font-weight:bold;color:blue;">'alphabet'</span>, attribute <span style="font-weight:bold;color:blue;">'letters'</span>: The value 'XYZ' does not match the fixed value constraint 'ABC'. 
<span style="font-weight:bold;color:red;">Line 185</span>: Element <span style="font-weight:bold;color:blue;">'drink'</span>: The attribute <span style="font-weight:bold;color:blue;">'coffee'</span> is required but missing. 
<span style="font-weight:bold;color:red;">Line 254</span>: Element <span style="font-weight:bold;color:blue;">'timeout'</span>: This element is not expected. 

相反的風格attribut的,你可以使用一個CSS類。

s#(Line \d+)#<span class="bold_red">$1</span>#; 
+0

所以我試圖在單詞「元素」後面的文本突出顯示,但它似乎比我需要的高。 我希望能夠在「元素」和「屬性」之後立即對單詞進行高亮度處理。下面的命令似乎是「元素:'aplhabet',而我只想'字母'突出顯示 $ output =〜s#(Element(\ s)['](\ w)*( - )*(\ w) * ['])# $ 1 #g; – sandster 2010-12-21 17:57:33

+0

@sandster:我編輯了我的答案 – Toto 2010-12-22 08:47:23