sub fix_xref { my ($parts, $text, $ref, $action, $link_text) = @_; $parts =~ s@(\d+(?:\.\d+)?(?:\([\w\d]+\))*)@s. $ref@g; return qq@$parts $text $link_text@; } while () { s@s\. (.+?) (of(?: the)) ([^<]+)@fix_xref($1,$2,$3,$4,$5)@eg; print; print "\n"; } __DATA__ s. 3 and 4 of the Interpretation Act s. 3 or 5 of the Interpretation Act s. 3, 4, 5, 6, 7 or 8 of the Interpretation Act