DERP, good point; for some reason I read that as having the matched number in it. Handwaving attempt below using /e and adding a bit of logic to the RHS of the substitution would work. Still has the problem of the fuzziness that it's non-trivial to be sure you're only pulling apartment numbers with just the regex and not getting years or what not.
A better solution would be to annotate explicitly in the source text as mentioned elsewhere. Better still use a real templating solution to explicitly mark up things. (e.g. TT like See more about [% apt_link( 1002 ) %] )
#!/usr/bin/env perl
use 5.034;
my $text = <<'EOT';
This is an appt: 1203
So is 1001.
But 8675 is not.
EOT
chomp($text);
my $digits_4_re = qr{ (?<!-)\b[0-9]{4}\b(?!-) }xms;
my %valid_appt_numbers = ( 1001 => 1, 1203 => 1, );
sub _anchor_if_apt {
my $candidate = shift;
if ( exists $valid_appt_numbers{$candidate} ) {
return _anchor_for_num($candidate);
} else {
return $candidate;
}
}
sub _anchor_for_num {
my $unit = shift;
return qq{<a href="apartment.pl?do_what=view&unit=$unit"><b>$unit<
+/b></a>};
}
$text =~ s{($digits_4_re)}{ _anchor_if_apt( $1 ) }xsmeg;
say $text;
exit 0;
__END__
$ perl texty.plx
This is an appt: <a href="apartment.pl?do_what=view&unit=1203"><b>1203
+</b></a>
So is <a href="apartment.pl?do_what=view&unit=1001"><b>1001</b></a>.
But 8675 is not.
The cake is a lie.
The cake is a lie.
The cake is a lie.