#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $string = '?!?!?!/pack/something/whatever.cacshdska'; my ($before, $after) = $string =~ /(...)\W.*something.*\.c(.*)/; say for $before, $after;