$ cat t.pl use strict; use warnings; while () { if (m".*?/(.*?)/" and $1 !~ m"gallery"i) { print "Match: $_"; } else { print "No match: $_"; } } __DATA__ foo bar/baz/boffo bar foo/gallary/gallery bim bam/gallery/blam/blim arg blarg/gallery/flarg $ perl t.pl Match: foo bar/baz/boffo Match: bar foo/gallary/gallery No match: bim bam/gallery/blam/blim No match: arg blarg/gallery/flarg