perl compare two strings and highlight mismatch characters -


string1 = "aaabbbbbcccccddddd" string2 = "aeabbbbbcceccddddd" 

output. mismatch (in case e) replaced html tags around e color it.

a**e**abbbbbcc**e**ccddddd 

what tried far: xor, diff , substr. first need find indices replace indices pattern.

use strict; use warnings; $string1 = 'aaabbbbbcccccddddd'; $string2 = 'aeabbbbbcceccddddd'; $result = ''; for(0 .. length($string1)) {     $char = substr($string2, $_, 1);     if($char ne substr($string1, $_, 1)) {         $result .= "**$char**";     } else {         $result .= $char;     } } print $result; 

prints a**e**abbbbbcc**e**ccddddd

tested somewhat. may contain errors.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -