#!/usr/bin/perl -w use strict; while () { $_ =~ /(The seller has the following \*fruit\* types: )\(?([a-z ,*]+)\)?\./; my ($const, $rep) = ($1, $2); $rep =~ s/\s?\*\s?/#/g; $rep =~ s/,? and/and/; print "$const*($rep)*.\n"; }