The more detail we know about the problem, the great the likely hood of success! I tried again with a couple of simple approaches...shown below..

Approach #1: All solutions must have the sequence of "tex" followed by "exception" or "exceptions". So the first grep (read these grep "stacks" from the bottom up), takes care of that situation. Then the next grep says if "tex" occurs more than once, then this is a bad line. I don't know if "tex:tex:exception" occurs or not? If so then this approach would filter that out. But if 'tex' only can occur once then this works fine.

Approach #2: Starts the same as a Approach #1, but the second grep says that if "tex" follows exception(s) then this is a "bad line" (remember the first grep{} assured that we are looking at a line that has a "tex..blah..exceptions", here we are looking to see if some 'tex' follows that exception part, and if so filter it out.

I don't see the need for any fancy look ahead/behind voodoo. Yes, there is a place and a situation for that, but I would go with something simple to understand. If this doesn't do what you want, then modify the @data and the #desired is comment section to more accurately describe what you need.

#!/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

In reply to Re^3: strange behavior of grep with global match by Marshall
in thread strange behavior of grep with global match [resolved] by ig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.