簡體   English   中英

使用Perl編輯HTML文件

[英]Use Perl to edit HTML files

下面的輸出是由第三方工具生成的,該工具根據XML Schema驗證XML文件。 它將輸出驗證錯誤。 好的,那只是一個小背景。

從上面顯示為HTML的錯誤中,我希望能夠使用perl執行“語法突出顯示”。 例如,我希望能夠突出顯示上面文本的某些部分。

具體來說,我想將符合“第[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.

嘗試一下:

#!/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.

可以使用CSS類代替樣式屬性。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM