0

I need to write function which takes a count and a string and return a list of all of the words in the string that are count word characters long or longer.

My function is:

import re

def find_words(count, a_str):
    count = int(count)
    return re.findall(r'\w{},'.format(int(count)), a_str)

But it doesn't work, it is return empty list:

Example:

find_words(4, "dog, cat, baby, balloon, me")

Should return:

['baby', 'balloon']

3 Answers 3

3

The regex isn't correct. The {} is interpreted as placeholder for format, but you want it to be the regexs' {} which specifies the number of repeats. You need to use r'\w{{{}}}' here. Observe the difference:

>>> r'\w{},'.format(4)
'\\w4,'

>>> r'\w{{{},}}'.format(4)
'\\w{4,}'

And then it works correctly:

import re
def find_words(count, a_str):
    count = int(count)
    return re.findall(r'\w{{{},}}'.format(count), a_str)

>>> find_words(4, "dog, cat, baby, balloon, me") 
['baby', 'balloon']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but why it is needed {{{}}} instead of just {{}}?
@Dmitriy_kzn The docs say "If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}." "Format String Syntax" And you want to keep one brace character in the literal text for the regex but you also want to insert the count. So {{ so it keeps the { and one { for the formatting.
2

Why RegExp?

>>> string = "dog, cat, baby, balloon, me"
>>> [word for word in string.split(', ') if len(word) >= 4]
['baby', 'balloon']

So function could be something like follow:

>>> def find_words(count, a_str):
...     return [word for word in a_str.split(', ') if len(word) >= count]
...
>>> find_words(4, 'dog, cat, baby, balloon, me')
['baby', 'balloon']

1 Comment

This is better than the regex version if you input a silly but valid count, like 0 or -1
0

You can try this:

def find_words(count, a_str):
   s = [re.findall("\w{"+str(count)+",}", i) for i in ["dog, cat, baby, balloon, me"]]
   return s[0]

print(find_words(4, ["dog, cat, baby, balloon, me"]))

Output:

['baby', 'balloon']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.