in reply to Excluding Words in RegEx

I would like to skip the line if it contains a word “group”. This word can occur anywhere in that line. How do I achieve in a single regex?
use Modern::Perl; while (<DATA>){ print $_ unless /\bgroup\b/; } __DATA__ This line is OK This line should be skipped: 'group' Drop this group too But keep this grouped content
This is so simple, I think there must be more than your simple requirement.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Excluding Words in RegEx
by Marshall (Canon) on Nov 06, 2011 at 11:28 UTC
    Well, This is so simple, I think there must be more than your simple requirement.
    I think so too!
    I don't see any need to use Modern::Perl. I figure old fashioned Perl will work just fine.
    #!/usr/bin/perl -w use strict; while (<DATA>) { print unless /\bgroup\b/; } =prints: This line is OK But keep this grouped content =cut __DATA__ This line is OK This line should be skipped: 'group' Drop this group too But keep this grouped content
      I always start all my scripts with use Modern::Perl. It is less typing than use strict; use warnings; and it switches on all the "modern" features too.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^2: Excluding Words in RegEx
by Lotus1 (Vicar) on Nov 06, 2011 at 16:11 UTC

    In the OP an example line starting with a four digit number is given. The OP gives a regex for capturing the number and the rest of the line separately but is asking how to skip lines containing 'group' at the same time. ikegami gave a good response.

      That is exactly why I said that the requirement could not have been that simple. Another example of an "XY-problem"!

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      It is not that simple I suppose. But ikegami code works great.

      my $Line = "1243 That will efficiently match a nonempty group with mat +ching parentheses two levels deep or less."; if ($Line =~ /^(?!.*\bgroup\b)(\d{4}) ([^.]+?[.\!\?])$/){ print "$1\n$2\n"; #Does not print } elsif ($Line =~ /^(?!.*\bgXroup\b)(\d{4}) ([^.]+?[.\!\?])$/){ print "$1\n$2\n"; #Prints }

      Though it solves my original query just wondering if it is possible to check the word in the second group of (). Thanks ramprasad27 for the "\." point. I didnt know that earlier.

      -Dominic