sub link_numbers ($$) { my $data = $_[0]; my $link = $_[1]; $data =~ s/([s]?s\. )?(\d+(?:\.\d+)?(?:\([^\)]+\))*)/$1$2<\/JL>/g; return $data; } while ($line = ) { $line =~ s/ ((?:(?:[s]?s\. )?\d+(?:\.\d+)?(?:\([^\)]+\))*(?:, | and | or )?)+)((?: of| the|,)+ )(]+)\">)/" " . link_numbers($1,$4) . "$2$3"/ge; print $line; print "\n"; } __DATA__ This can be found in ss. 3 and 4 of the Interpretation Act. As discussed in s. 3 or 5 of the Interpretation Act. Filling up space in ss. 3, 4, 5, 6, 7 or 8 of the Interpretation Act .