简体   繁体   中英

int doesn't work in def within regular expression in python

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']

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']

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']

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']

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