regex - Identifying Difference in string using Perl -
i have 2 stings (sentences) , identify specific difference in both strings (sentences) when not match.
my sample code below
my $diffpara1 = "this paragraph 1"; $diffpara2 = "this paragraph 2 different first paragraph"; $samepara1 = "this paragraph same"; $samepara2 = "this paragraph same"; print (($diffpara1 eq $diffpara2) ? '<span style="background-color: green">matching</span>' : '<span style="background-color: red">not matching</span>'); print "<br/>".(($samepara1 eq $samepara2) ? '<span style="background-color: green">matching</span>' : '<span style="background-color: red">not matching</span>');
the result above code is:
the above indicates if strings (sentences) match or if strings (sentences) doesn't match. generate output indicates different in both strings (sentence).
example output want (bold difference):
this paragraph 1
paragraph 2 different first paragraph
i not sure if can use regex obtain required output.
thanks in advance help.
try text::worddiff. can output differences html, deleted , inserted sections marked <del>
, <ins>
tags respectively. quick example:
use strict; use warnings; use feature ":5.10"; use text::worddiff; $diffpara1 = "this paragraph 1"; $diffpara2 = "this paragraph 2 different first paragraph"; # output difference between lines html, on 2 lines: $diff = word_diff \$diffpara1, \$diffpara2, { style => 'htmltwolines' }; $diff;
output:
<div class="file"><span class="hunk">this paragraph </span><span class="hunk"><del>1</del></span></div> <div class="file"><span class="hunk">this paragraph </span><span class="hunk"><ins>2 different first paragraph</ins></span></div>
identical lines:
my $samepara1 = "this paragraph same"; $samepara2 = "this paragraph same"; $diff2 = word_diff \$samepara1, \$samepara2, { style => 'htmltwolines' }; $diff2;
output:
<div class="file"><span class="hunk">this paragraph same</span></div> <div class="file"><span class="hunk">this paragraph same</span></div>
there numerous different output options (save plain text, save html, save file, save variable, etc.) , can configure html version show inserted , deleted text in different colours, in bold, or want using almighty power of css.
Comments
Post a Comment