tags:

views:

126

answers:

2

Hello

Please help with below

I need match only words where counting of characters same

for example same counting for a b c

abc ///match 1 (abc)

aabbcc match 2(abc)

adabb not mach 2(ab)

ttt match 0(abc)

A: 

You should use a recursive regular expression.

Below is the Perl code for matching the same number or 0s and 1s

$regex = qr/0(??{$regex})*1/;

 

NB: for more backround, please refer to Recursive Regular Expressions on Peteris Krumins's blog.

Adrian
A: 

Why are you using regular expressions for this? Regular expressions are the right tool for some jobs but they are overused where plain old string processing would do the trick, possibly with greater clarity or efficiency. Here's a sample implemented in Python:

def matchCount(inputString, lettersToMatch, count) :
    matches = []

    wordsArray = inputString.split()
    for word in wordsArray:
        letterCounts = {}
        for letter in word:
            if letter in letterCounts:
                letterCounts[letter] += 1
            else:
                letterCounts[letter] = 1

        allCorrect = True
        for letter in lettersToMatch:
            if letter !in letterCounts:
                allCorrect = False
            if letterCounts[letter] != count:
                allCorrect = False

            if !allCorrect:
                break

       if allCorrect:
           matches.append(word)
 return matches
dsimcha