#!/usr/bin/perl -w $string="foobar"; if ($string =~ /(f(o(o(b(a(r)?)?)?)?)?)$/) { print "$1\n"; print "$2\n"; print "$3\n"; } #### foobar oobar obar