Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regexp that matches half then conditionally excludes the other half

Reply
Thread Tools

regexp that matches half then conditionally excludes the other half

 
 
Mike Ballard
Guest
Posts: n/a
 
      11-15-2005

I have to search for string pairs such as "Kc Qd" and "Qh Ks" and am
having a problem figuring out a regexp that will exclude (for example):

"Kd Qd"
"Kd Kc"
"Qd Qh"
"Qh Kh"

IOW, both uppercase letters in each pair must be unique (discard "Kx Kx",
"Qx Qx") and the lowercase letters for each pair must be unique (discard
"Ks Qs", "Qc Kc", etc.). The range of lowercase letters is "cdhs".

So if the first part of a pair is "Kh" and the second part is "Kx" or
"Qh", the whole thing needs to be skipped and I move on to the next one.

Can someone tell me how to do this (it's in a perl script btw)?

Mike
--

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-15-2005
Mike Ballard wrote:
> So if the first part of a pair is "Kh" and the second part is "Kx" or
> "Qh", the whole thing needs to be skipped and I move on to the next one.


This is one way to do it:

#!/usr/bin/perl
use warnings; use strict;

for (<DATA>) {
/(.)(.) (.)(.)/;
next if $1 eq $3 || $2 eq $4;
print;
}

__DATA__
Kd Qd
Kd Kc
Qd Qh
Qh Kh
Kx Kx

 
Reply With Quote
 
 
 
 
Charlton Wilbur
Guest
Posts: n/a
 
      11-15-2005
>>>>> "MB" == Mike Ballard <dont_w@nt_spam.org> writes:

MB> Can someone tell me how to do this (it's in a perl script
MB> btw)?

Why do you insist on doing it entirely with a regular expression?
Make the logic explicit, and it will be that much easier to write and
to maintain.

if (/([KQ])([cdhs]) ([KQ])([cdhs])/ && $1 ne $3 && $2 ne $4)
{
....
}

Charlton


--
cwilbur at chromatico dot net
cwilbur at mac dot com
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-15-2005
Mike Ballard <dont_w@nt_spam.org> wrote in
news:m23bly7gi9.fsf@west_f1.net:

> I have to search for string pairs such as "Kc Qd" and "Qh Ks" and am
> having a problem figuring out a regexp that will exclude (for
> example):
>
> "Kd Qd"
> "Kd Kc"
> "Qd Qh"
> "Qh Kh"
>
> IOW, both uppercase letters in each pair must be unique (discard "Kx
> Kx", "Qx Qx") and the lowercase letters for each pair must be unique
> (discard "Ks Qs", "Qc Kc", etc.). The range of lowercase letters is
> "cdhs".
>
> So if the first part of a pair is "Kh" and the second part is "Kx" or
> "Qh", the whole thing needs to be skipped and I move on to the next
> one.
>
> Can someone tell me how to do this (it's in a perl script btw)?


By writing it, of course.

use strict;
use warnings;

while (<DATA>) {
if (m{([KQ])([cdhs]) ([KQ])([cdhs])}) {
if ($1 ne $3 and $2 ne $4) {
print "$1$2 $3$4\n";
}
}
}


__DATA__
Kd Qd
Kd Kc
Qd Qh
Qh Kh
Kd Qc



--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-15-2005
Mike Ballard wrote:
> I have to search for string pairs such as "Kc Qd" and "Qh Ks" and am
> having a problem figuring out a regexp that will exclude (for example):
>
> "Kd Qd"
> "Kd Kc"
> "Qd Qh"
> "Qh Kh"
>
> IOW, both uppercase letters in each pair must be unique (discard "Kx Kx",
> "Qx Qx") and the lowercase letters for each pair must be unique (discard
> "Ks Qs", "Qc Kc", etc.). The range of lowercase letters is "cdhs".
>
> So if the first part of a pair is "Kh" and the second part is "Kx" or
> "Qh", the whole thing needs to be skipped and I move on to the next one.
>
> Can someone tell me how to do this (it's in a perl script btw)?


/([A-Z])([a-z]) (?!\1[a-z])(?![A-Z]\2)/


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Mike Ballard
Guest
Posts: n/a
 
      11-15-2005

On Tue Nov 15, I was peacefully napping until Mike Ballard said:

> I have to search for string pairs such as "Kc Qd" and "Qh Ks" and am
> having a problem figuring out a regexp that will exclude (for example):
>
> "Kd Qd"
> "Kd Kc"
> "Qd Qh"
> "Qh Kh"
>
> IOW, both uppercase letters in each pair must be unique (discard "Kx Kx",
> "Qx Qx") and the lowercase letters for each pair must be unique (discard
> "Ks Qs", "Qc Kc", etc.). The range of lowercase letters is "cdhs".
>
> So if the first part of a pair is "Kh" and the second part is "Kx" or
> "Qh", the whole thing needs to be skipped and I move on to the next one.
>
> Can someone tell me how to do this (it's in a perl script btw)?
>


Thanks all, very much.

I'm glad I asked the question the way I did - learned some pretty cool
stuff (for me).

Really appreciate the help...

Mike
--
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      11-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed):
> Mike Ballard:


>> So if the first part of a pair is "Kh" and the second part is "Kx" or
>> "Qh", the whole thing needs to be skipped and I move on to the next
>> one.

>
> This is one way to do it:



I am missing the limits on uppercase and [cdhs].

#!/usr/bin/perl
use warnings; use strict;

for (<DATA>) {
next if ! /^([[:upper:]])([cdhs]) ([[:upper:]])([cdhs])$/
|| $1 eq $3
|| $2 eq $4;
print;
}

__DATA__
Kd Qd
Kd Kc
Kd Qc
dK cQ
Qd Qh
Qh Kh
Kx Kx

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
py2exe Excludes Ed Leafe Python 2 01-13-2005 12:55 PM
AVG Excludes? mhicaoidh Computer Support 2 08-12-2003 06:00 PM



Advertisments