regexp that matches half then conditionally excludes the other half

M

Mike Ballard

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
--
 
U

usenet

Mike said:
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
 
C

Charlton Wilbur

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
 
A

A. Sinan Unur

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
 
J

John W. Krahn

Mike 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)?

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


John
 
M

Mike Ballard

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
--
 
D

Dr.Ruud

(e-mail address removed):
Mike Ballard:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Beginner Question--rand( ) 23
Sizewell B++ 0
Rods 0
Lowering Of Graphite Rods Into Reactor Core 1

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top