/path/to/some/file #### #!/usr/bin/env perl -l use strict; use warnings; my $re = qr{file:(.*?).xml}; while () { print $1 if /$re/; } __DATA__ file:/path/to/someA/file.mxf file:/path/to/someB/file.xml #### /path/to/someB/file #### #!/usr/bin/env perl -l use strict; use warnings; my $re = qr{file:(.*?\.xml)}; while () { print $1 if /$re/; } __DATA__ file:/path/to/someA/file.mxf file:/path/to/someB/file.xml #### /path/to/someB/file.xml