regexp that matches half then conditionally excludes the other half

Discussion in 'Perl Misc' started by Mike Ballard, Nov 15, 2005.

  1. Mike Ballard

    Mike Ballard Guest

    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
    --
     
    Mike Ballard, Nov 15, 2005
    #1
    1. Advertising

  2. Mike Ballard

    Guest

    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
     
    , Nov 15, 2005
    #2
    1. Advertising

  3. >>>>> "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
     
    Charlton Wilbur, Nov 15, 2005
    #3
  4. 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 15, 2005
    #4
  5. Re: regexp that matches half then conditionally excludes the otherhalf

    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
     
    John W. Krahn, Nov 15, 2005
    #5
  6. Mike Ballard

    Mike Ballard Guest

    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
    --
     
    Mike Ballard, Nov 15, 2005
    #6
  7. Mike Ballard

    Dr.Ruud Guest

    :
    > 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."
     
    Dr.Ruud, Nov 15, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Leafe

    py2exe Excludes

    Ed Leafe, Jan 12, 2005, in forum: Python
    Replies:
    2
    Views:
    827
    Ed Leafe
    Jan 13, 2005
  2. trint
    Replies:
    4
    Views:
    724
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Sep 11, 2007
  3. Replies:
    0
    Views:
    338
  4. Joao Silva
    Replies:
    16
    Views:
    406
    7stud --
    Aug 21, 2009
  5. joe chesak
    Replies:
    7
    Views:
    310
    (r.*n){2}
    Sep 23, 2010
Loading...

Share This Page