Negating phrases 
Author Message
 Negating phrases

Hi

If I have code like this:


{
     print "$_\n" if /Name is [^Billy|^Fred]/;

Quote:
}

What I want it to do is print out all lines, except those where the name is
Billy or Fred. The above doesn't work, as it doesn't print out the Freda
line.

Could you tell me what I'm doing wrong?

Thanks

Allanon



Sat, 29 Oct 2005 12:25:22 GMT  
 Negating phrases

Quote:
> Hi

> If I have code like this:



> {
>      print "$_\n" if /Name is [^Billy|^Fred]/;
> }

> What I want it to do is print out all lines, except those where the name is
> Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> line.

> Could you tell me what I'm doing wrong?

You're ignoring how character classes "[]" work in a regex.  Try this
instead (untested):

    print unless /Name is (?:Billy|Fred)/;

Anno



Sat, 29 Oct 2005 12:37:36 GMT  
 Negating phrases

Quote:

> Hi

> If I have code like this:



> {
>      print "$_\n" if /Name is [^Billy|^Fred]/;

                                ^^^^^^^^^^^^^^
This is a char class, matching one of the given chars.

[^B] means match a char that is not B

btw, I don't know what the second occurencs of ^ does.

Quote:
> }

> What I want it to do is print out all lines, except those where the name is
> Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> line.

negative lookahead could do the job:

     print "$_\n" if /Name is (?!Billy|Freda)/;

lg,
        tom



Sat, 29 Oct 2005 12:38:21 GMT  
 Negating phrases


Quote:

> > Hi

> > If I have code like this:


Steve");


> > {
> >      print "$_\n" if /Name is [^Billy|^Fred]/;
> > }

> > What I want it to do is print out all lines, except those where the name
is
> > Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> > line.

> > Could you tell me what I'm doing wrong?

> You're ignoring how character classes "[]" work in a regex.  Try this
> instead (untested):

>     print unless /Name is (?:Billy|Fred)/;

Yes, I guess that would work.. however, I wanted to handle the negation
purely inside the regular expression.

Allanon



Sat, 29 Oct 2005 13:49:32 GMT  
 Negating phrases

Quote:

> > Hi

> > If I have code like this:


Steve");


> > {
> >      print "$_\n" if /Name is [^Billy|^Fred]/;
>                                 ^^^^^^^^^^^^^^
> This is a char class, matching one of the given chars.

> [^B] means match a char that is not B

> btw, I don't know what the second occurencs of ^ does.

> > }

> > What I want it to do is print out all lines, except those where the name
is
> > Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> > line.

> negative lookahead could do the job:

>      print "$_\n" if /Name is (?!Billy|Freda)/;

Thanks.. that works with a slight modification.. ie: I originally wrote
Fred, not Freda, as the second exclusion.. Fred excludes both Fred & Freda,
so I'd need to write the line as:

print "$_\n" if /Name is (?!Billy$|Freda$)/;

to make sure I get the desired results for any names that might be there.

Allanon



Sat, 29 Oct 2005 14:01:24 GMT  
 Negating phrases

Quote:

> Hi

> If I have code like this:



> {
>      print "$_\n" if /Name is [^Billy|^Fred]/;
> }

> What I want it to do is print out all lines, except those where the name is
> Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> line.

> Could you tell me what I'm doing wrong?

> Thanks

> Allanon

Wouldn't it be easier to use grep?




Sat, 29 Oct 2005 14:41:27 GMT  
 Negating phrases

Quote:

> Could you tell me what I'm doing wrong?

Starting a new thead with question substancially the same as was
discussed in the thread "dont match this pattern" less than a week
ago.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Sat, 29 Oct 2005 17:58:13 GMT  
 Negating phrases


Quote:

> > Could you tell me what I'm doing wrong?

> Starting a new thead with question substancially the same as was
> discussed in the thread "dont match this pattern" less than a week
> ago.

Damn, you're right.. I didn't spot that one.. I guess, it's off to the firey
pits of hell for me then! ;)

Allanon



Sun, 30 Oct 2005 10:01:33 GMT  
 Negating phrases
In our last episode, the evil Dr. Lacto had captured our hero,

Quote:
>>      print "$_\n" if /Name is [^Billy|^Fred]/;

                                ^^^^^^^^^^^^^^

Quote:
>This is a char class, matching one of the given chars.

>[^B] means match a char that is not B

>btw, I don't know what the second occurencs of ^ does.

When a ^ is not the first character after the opening bracket, then it
has no special meaning, it is simply another character in the class.


===============================================================================



Sun, 30 Oct 2005 19:47:23 GMT  
 Negating phrases

Quote:

>>     print unless /Name is (?:Billy|Fred)/;

>Yes, I guess that would work.. however, I wanted to handle the negation
>purely inside the regular expression.

Eh? That *is* inside the regular expression.

--
        Bart.



Mon, 31 Oct 2005 10:49:51 GMT  
 Negating phrases
On Thu, 15 May 2003 09:49:51 GMT

Quote:


> >>     print unless /Name is (?:Billy|Fred)/;

> >Yes, I guess that would work.. however, I wanted to handle the negation
> >purely inside the regular expression.

> Eh? That *is* inside the regular expression.

> --
>    Bart.

the negation is not in the regexp, it's in the "unless", right?

i don't see why it matters, but maybe he could use:
print if /Name is (?!Billy|Fred(?!.))/;

btw, the suggested solution would work perfectly if it were anchored:
print unless /Name is (?:Billy|Fred)$/;
otherwise Freda matches.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail



Mon, 31 Oct 2005 11:35:07 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. 'Flatten' array of phrases

2. code phrases

3. ignoring lines in file using array of phrases to skip

4. Perl script (CGI) to automate keying in of pass phrase

5. ok, DBI alt row colors - phrased better w/code

6. word and phrase frequency count

7. How to count words and phrases in text files

8. How to find phrases in long texts?

9. Matching a phrase

10. Phrase Capitalization

 

 
Powered by phpBB® Forum Software