简体   繁体   中英

How to match pairs of characters using Regex?

I have a variable containing a string. This string contains only alphabetic and numeric characters. This string also have a fixed length by 32 characters in length. How I can match using regular expressions if this string have only paired characters by length of 2 , 4 , 8 , 16 ?

For example, for similar to this strings:

abcdefghijklmnopqrstuvwxyz012345

Regex.IsMatch must return false .

But for strings similar to this:

aaaaaaaaaaaaaaaa 5555555555555555

this is a 16 -characters pairs;

aaaaaaaa 55555555aaaaaaaa55555555

this is a 8 -characters pairs;

aaaa 5555aaaa5555aaaa5555aaaa5555

this is a 4 -characters pairs;

aa 55aa55aa55aa55aa55aa55aa55aa55

this is a 2 -characters pairs -

Regex.IsMatch must return true .

EDIT

Apparently, the requirement is simply to match eg aabbccddeeffgghhiijjkkllmmnnoopp , ie the first two characters must be the same, then the next two etc for exactly 32 characters. That can be easily tested for with:

((\w)\2(\w)\3){8}

This should work (without needing to come up with individual regexes for each possible combination).

public bool isRelevantMatch(string inputString)
{
    int matchCount = Regex.Matches(inputString, @"([a-zA-Z])\1{1}").Count;
    return matchCount == 1 || 
           matchCount == 2 || 
           matchCount == 4 || 
           matchCount == 8 ||
           matchCount == 16;
}

Explanation: get the count of matches of repeated characters (using a backreference regex to match any instance of aa, AA, bb, BB, etc.). If that count is 1, 2, 4, or 8, return true (there are 2, 4, 8, or 16 paired characters in the string).

Late to this but will throw out this.

If you want to do repeating ' unique pairs ' this works in Perl.
I tried to make this smaller but couldn't figure out how.

The syntax for Dot-Net is probably the same. However, I've reused the
capture group names, which works in Perl, but not sure about Dot-Net
(should be ok, if not change to unique names).

Also, in Perl, could have used a branch reset to overlay capture groups,
then test a single group length to get the repeat order, but this is not available in Dot-Net.
So, just have to test 4 groups for a match (or length) to get the order.

 # (?<A>(?<b>\w)\k<b>(?!\k<b>)(?<c>\w)\k<c>)\k<A>{7}|(?<A>(?<b>\w)\k<b>{3}(?!\k<b>)(?<c>\w)\k<c>{3})\k<A>{3}|(?<A>(?<b>\w)\k<b>{7}(?!\k<b>)(?<c>\w)\k<c>{7})\k<A>{1}|(?<A>(?<b>\w)\k<b>{15}(?!\k<b>)(?<c>\w)\k<c>{15}) 


   (?<A>                         # (1 start), 2 char pairs, repeating x 8
        (?<b> \w )                    # (2)
        \k<b> 

        (?! \k<b> )
        (?<c> \w )                    # (3)
        \k<c> 
   )                             # (1 end)
   \k<A>{7} 
|  
   (?<A>                         # (4 start), 4 char pairs, repeating x 4
        (?<b> \w )                    # (5)
        \k<b>{3} 

        (?! \k<b> )
        (?<c> \w )                    # (6)
        \k<c>{3} 
   )                             # (4 end)
   \k<A>{3} 

|  
   (?<A>                         # (7 start), 8 char pairs, repeating x 2
        (?<b> \w )                    # (8)
        \k<b>{7} 

        (?! \k<b> )
        (?<c> \w )                    # (9)
        \k<c>{7} 
   )                             # (7 end)
   \k<A>{1} 
|  
   (?<A>                         # (10 start), 16 char pairs
        (?<b> \w )                    # (11)
        \k<b>{15} 

        (?! \k<b> )
        (?<c> \w )                    # (12)
        \k<c>{15} 
   )                             # (10 end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM