The output below is generated by a third party tool which validates XML files against a XML Schema. It will output validation errors. Ok, so that was just a little context.
From the errors above are displayed in HTML, I want to be able to perform "syntax highlighting" using perl. For instance, I want to be able to highlight certain parts of the text above.
Specifically, I want to colour any text that conforms to "Line [0-9]*" as bold and in red. I've tried to experiment with regex search and replace but I've not been that success开发者_如何学JAVAful.
Any pointers/hints would be fantastic.
Thank you!
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.
Have a try with this:
#!/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.
output
<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.
Instead of style attribut, you can use a css class.
s#(Line \d+)#<span class="bold_red">$1</span>#;
精彩评论