$str = fraction($str); ... sub fraction { my $str = shift; if ($str =~ /\d+_\d+/) { # convert $str to fraction using mimetex syntax } return $str; }