#!/usr/bin/perl -w use strict; my @data = ('exception:mex', 'qwerty', 'tex:exception:mex', 'exception:mex', 'exception:tex', 'tex:exception', 'tex:exception:tex', 'tex : exceptions:mex', 'tex:exception:mex:tex', 'asdf'); #desired is: tex:exception:mex # tex:exception # tex : exceptions:mex my @matches = grep{ #approach #1 my @texes = m/tex/g; @texes <=1 } grep{/tex.*?exception(s)?/} @data; print join ("\n",@matches),"\n\n"; @matches = grep{ !/exception(s)?.*?tex/} #approach #2 grep{/tex.*?exception(s)?/} @data; print join ("\n",@matches),"\n"; __END__ Prints: tex:exception:mex tex:exception tex : exceptions:mex tex:exception:mex tex:exception tex : exceptions:mex