4

I fairly new to regular expressions and need some help. I need to filter some lines using regex in Perl. I am going to pass the regex to another function so it needs to be done in a single line.

I want to select only lines that contain "too long"and that don't begin with "SKIPPING"

Here are my test strings:

SKIPPING this bond since maturity too long
TKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
hello there

The regex rule should match the following on 'too long":

SKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long

and it should skip:

"hello there" because it doesn't contain 'too long'
"SKIPPING this bond since maturity too long" because it containst 'SKIPPING'

0

6 Answers 6

11
/^(?!SKIPPING).*too long/
2
  • THIS is how negative look-behind is done in Perl. No < sign. Aug 20, 2009 at 21:09
  • 4
    Actually, that's a negative lookahead. Negative lookbehind does indeed have a <... the syntax is (?<!pattern).
    – chaos
    Aug 20, 2009 at 21:13
10

Personally, I'd do this as two separate regex just to make it clearer.

while (<FILE>)
{
  next if /^SKIPPING/;
  next if !/too long/;

   ... do stuff
}
1
  • 9
    What do you mean "Not An Answer"? It accomplishes exactly what he wants, just not in precisely the way he thought to do it. To me, that's an answer. Aug 20, 2009 at 21:57
3

I suspect you maybe after a single regex however I prefer to split into something more readable like this:

use strict;
use warnings;

for my $line ( <DATA> ) {
    next  if $line =~ m/^SKIPPING/;
    next  if $line !~ m/too long/;

    # do something with $line
    chomp $line;
    say "Found: ", $line, ':length=', length( $line );
}

__DATA__
SKIPPING this bond since maturity too long
TKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
hello there
1

Use a lookahead; see this explanation of regex lookaround.

^(?!SKIPPING).*too long
4
  • That site is plain wrong about the syntax for negative look-behind, unfortunately. Aug 20, 2009 at 21:08
  • @Daniel: no, it's not. I think you are confused here.
    – ysth
    Aug 21, 2009 at 0:24
  • @Daniel: How is the site wrong? Negative lookbehind takes the form (?<!...) in every regex flavor I know (that supports lookbehind). Perhaps you're thinking of negative lookahead, (?!...).
    – Alan Moore
    Aug 21, 2009 at 0:26
  • @Alan M: The site is indeed right and my answer was a negative lookahead, but I thought it was worth mentioning that some (albeit very few) regex flavours use the syntax (...)\@<!
    – DrAl
    Aug 21, 2009 at 5:48
0
/^(?<!SKIPPING).*too long$/

Matches the lines you're looking for. The dollar sign at the end causes it to match only strings that end with "too long".

Hope this helps!

1
  • worked for me in JS. Exlamation symbol (!) did not work in my javascript code.
    – linuxeasy
    Feb 2, 2022 at 9:22
-2

Using negative lookbehind:

(?<!^SKIPPING)too long$
3
  • I'm unsure about the ?<!^ syntax, but it really ought to look like: (?<!^SKIPPING).*too long Aug 20, 2009 at 20:57
  • You can't use a variable-length lookbehind.
    – Axeman
    Aug 20, 2009 at 21:56
  • @Jeremy Powell: no, that matches because there are several places where (?<!^SKIPPING) is true, even if the line doesn't begin with SKIPPING.
    – ysth
    Aug 21, 2009 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.