Author |
Message |
Allano #1 / 11
|
 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 |
|
 |
Anno Sieg #2 / 11
|
 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 |
|
 |
Thomas Haselberge #3 / 11
|
 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 |
|
 |
Allano #4 / 11
|
 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 |
|
 |
Allano #5 / 11
|
 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 |
|
 |
Andras Malatinszk #6 / 11
|
 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 |
|
 |
Brian McCaule #7 / 11
|
 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 |
|
 |
Allano #8 / 11
|
 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 |
|
 |
hymi #9 / 11
|
 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 |
|
 |
Bart Lateu #10 / 11
|
 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 |
|
 |
John Straus #11 / 11
|
 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 |
|
|