In addition to the negative look-ahead, you must escape all the html mark-up characters which have special meaning in a substitution string. Normally you could use \Q and \E, but in this case, they would escape the $1's which must have their regex meaning. You also need a \d in the negative look-ahead to prevent matching the first three digits of the year. You probably should repeat this test with more realistic strings, but I think this covers the important cases. (All six cases pass)
use strict;
use warnings;
use Test::Simple tests => 6;
my $digits_4 = qr/(\d{3,4})(?![\d-])/;
my $hyper = '<hyper>';
my @cases = (
# Input Expected output
['2018-1-1 1234', '2018-1-1 <a href="script.pl?do_what=view&unit
+=1234"><b>1234</b></a>' ],
['2018-1-1 567', '2018-1-1 <a href="script.pl?do_what=view&unit
+=567"><b>567</b></a>' ],
['2018-1-1 abcd', '2018-1-1 abcd' ],
['2018-1-1 1234 x', '2018-1-1 <a href="script.pl?do_what=view&unit
+=1234"><b>1234</b></a> x' ],
['2018-1-1 567 x', '2018-1-1 <a href="script.pl?do_what=view&unit
+=567"><b>567</b></a> x' ],
['2018-1-1 abcd x', '2018-1-1 abcd x' ],
);
foreach my $case (@cases) {
$case->[0] =~ s{$digits_4}
{\<a href\=\"script\.pl\?do\_what\=view\&unit\=$1\"\>\<b\>$1\<
+\/b\>\<\/a\>};
ok( $case->[0] eq $case->[1]);
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.