peleitor peleitor - 8 days ago 6
Python Question

Python regex for capturing all strings like (\w\s)+

I need to capture all strings like (\w\s), with a minimum length of 3, with a Python regex for findall method. This is: to find as many "words" (substrings) as possible, which are composed of a concatenation of at least 3 .

For example, for:

This is an e x a m p l e about T H I S question.


findall should return:

["e x a m p l e ", "T H I S "]


Already tried with:

\S\S+(\w\s+){3,}\S+


Thanks

Answer

Use the following approach with specific regex pattern:

s = 'This is an e x a m p l e about T H I S question.'
result = re.findall(r'\b((?:[\w]\s){3,})', s)

print(result)

The output:

['e x a m p l e ', 'T H I S ']
Comments