2010-12-21 13 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コードの一部と正規表現を表示する必要があります。 –

+0

上記のテキストは、HTMLファイルに表示されるものです。それは私が – sandster

+0

で強調表示を実行したいと思っているものですあなたはそこにはHTMLがないことを認識していますよね?テキストは 'pre'ブロックにありますか? –

答えて

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. 

出力する代わりに、スタイルattributの

<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>#; 
+0

"Element"という単語の後にテキストの強調表示を実行しようとしましたが、必要以上に高いようです。 "Element"と "attribute"の直後に単語をハイライトしたいと思っています。以下のコマンドは、「Element: 'aplhabet'」を強調表示したいのに対し、「アルファベット」はハイライト表示したいと考えています。 $ output =〜s#(要素(\)*( - )*(\ w) * ['])# $ 1 #g; – sandster

+0

@sandster:私の回答を編集しました – Toto

関連する問題