regex negating a search/word 
Author Message
 regex negating a search/word

Hello,

Given the following regex:

$file2 =~ /TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d)\D+/s;$P33A23 = $1

I want a way to have the search fail or to stop the search if it
finds "TABLE 71".

Any ideas how I might accomplish this?

Lance



Tue, 18 Oct 2005 15:29:56 GMT  
 regex negating a search/word


Quote:
> Hello,

> Given the following regex:

> $file2 =~ /TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d)\D+/s;$P33A23 = $1

> I want a way to have the search fail or to stop the search if it
> finds "TABLE 71".

> Any ideas how I might accomplish this?

Check index($file2, "TABLE 71") before doing the regex, or use a
zero-width negative look-ahead assertion by inserting (?!.*TABLE 71)
at the beginning of your match pattern.

perldoc perlre

You'll probably only want to assign to $P33A23 if the match succeeded,
though.

Hunter
--
J. Hunter Johnson        | "A little consistent wholesome modeling and

(937) 865-6800 x55385    |    true words harshly spoken."
Lexis-Nexis, Dayton, OH  |                               -- Ron Sider



Tue, 18 Oct 2005 16:27:04 GMT  
 regex negating a search/word

Quote:

> Hello,

> Given the following regex:

> $file2 =~ /TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d)\D+/s;$P33A23 = $1

> I want a way to have the search fail or to stop the search if it
> finds "TABLE 71".

> Any ideas how I might accomplish this?

> Lance

Try a sentinel, for example like this:

   my $stopit;
   my $file2;
   my $P33A23;


        if ( $file2 =~ /TABLE\s+71/ ) {
                $stopit = 1;
        } else {
                print($file2 . "\n"); # just for testing purposes
                $file2 =~ /TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d)\D+/s;
                $P33A23 = $1;
        }
   }

If the numerical "stopping" value is just greater than 70 (and not
necessarily exactly 71), then use the following if-clause instead:

        if ( ($file2 =~ /TABLE\s+(\d\d)/) && $1 > 70 ) {
                $stopit = 1;
        }

Regards,

   [mj]



Tue, 18 Oct 2005 18:40:15 GMT  
 regex negating a search/word

Quote:

> Hello,

> Given the following regex:

> $file2 =~ /TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d)\D+/s;$P33A23 = $1

> I want a way to have the search fail or to stop the search if it
> finds "TABLE 71".

> Any ideas how I might accomplish this?

> Lance

Hello Lance:

I'm not sure I understand your entire objective here, but I presume
this line is inside a block such as

  open F, $someFile;
  while <F>
   {last if /TABLE\s+71/; # add this line to short-circuit the search?
    $file2 =~ /TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d)\D+/s;
    $P33A23 = $1;
   }

It seems odd to loop though a whole file to store one value. So lets
look at another tact (I havent tested this code, beware it's FRIDAY
and almost beer-thirty)..

  open F, $someFile;

# snag the file file cram it into a scalar, close it to be tidy

  close F;

# now truncate the inputfile at /TABLE\s+71/
  $f =~ s/TABLE\s+71.+$//sm;
  $f =~ s/^.*TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d).+$/$1/;

# at this point $f either contains $1, if it matched, or the truncated
file if
# it didn't. There are a few ways to test that, perhaps another regex?

  $f = '' unless $f =~ /^\d{1,3}\.\d$/;

Again I'm not certain this is what you were after, if not please
disregard. However, note that this approach is loop-less, which can be
desirable. Dont forget your old friends Mr "Strict" and Mr warnings
either!

Finally- that variable $P33A23 looks very odd, as if perhaps its
related to some coordinate like (33,23) or something like that? If so,
and you have others with similar names, I recommend a hash for them
like

 my $p = {};
  .
  .
 $p->{23}={};
 $p->{23}{33} = blah blah...

But again, I may be misinterpreting between the lines of your post.

Have a great Perl weekend! Oh wait I forgot the obligatory flame that
the other poster said all contributors give here. Let's see.. *think*
*think*

Ahhh forget it I need a beer!

-Gx



Tue, 18 Oct 2005 21:32:35 GMT  
 regex negating a search/word

Quote:

> # now truncate the inputfile at /TABLE\s+71/
>   $f =~ s/TABLE\s+71.+$//sm;
>   $f =~ s/^.*TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d).+$/$1/;

> # at this point $f either contains $1, if it matched, or the truncated
> file if
> # it didn't. There are a few ways to test that, perhaps another regex?

Ack, no.  Just test the result of the one you just applied:

if($f =~ s/$regex/) {
  <do stuff with $1>

Quote:
} else {

  <complain about not matching, or whatever>

Quote:
}

--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
                -- Blair Houghton.


Tue, 18 Oct 2005 21:35:46 GMT  
 regex negating a search/word

Quote:


> > # now truncate the inputfile at /TABLE\s+71/
> >   $f =~ s/TABLE\s+71.+$//sm;
> >   $f =~ s/^.*TABLE\s+70.*?TELEVISION.*?(\d{1,3}\.\d).+$/$1/;

> > # at this point $f either contains $1, if it matched, or the truncated
> > file if
> > # it didn't. There are a few ways to test that, perhaps another regex?

> Ack, no.  Just test the result of the one you just applied:

> if($f =~ s/$regex/) {
>   <do stuff with $1>
> } else {
>  <complain about not matching, or whatever>
> }

Eric:

Quite right.. Perhaps the simplest:

  if (defined $1)
   {do stuff}
  else
    {whine();}



Wed, 19 Oct 2005 21:03:47 GMT  
 regex negating a search/word

Quote:

> Quite right.. Perhaps the simplest:

>   if (defined $1)
>    {do stuff}
>   else
>     {whine();}

That's even worse-- sometimes it will work, and then it will
mysteriously stop working, and you won't know why.  You MUST test the
result of the match, as $1 and friends will remain defined from a
previous match, if one happened:

$ cat /tmp/bah.pl
#!/usr/bin/perl

$str = "this is a test";
$str =~ /this is a (\w+)/;

print "$1\n" if defined $1;

$str =~ /this is not a (\w+)/;

print "$1\n" if defined $1;

$ perl /tmp/bah.pl
test
test

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
                -- Blair Houghton.



Fri, 21 Oct 2005 20:32:37 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. RegEx and Negated Words

2. ms word search regex

3. Reg Exp - Negating a Word seems IMPOSSIBLE!

4. Can't Negate Regex /^8([0876])\1\d+$/

5. negate regex?

6. Regex question (negating a charater sequence).

7. How to: Create Regex which extracts N number of words before target word

8. regex for word whitespace word

9. Regex: deleting non-matching words

10. highlight words by regex in pdf files using perl

11. Regex matching specific words, in any order

12. Regex: optional word boundary

 

 
Powered by phpBB® Forum Software