revised code; switched to regular expressions