peleitor peleitor - 1 year ago 85
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:



Answer Source

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)


The output:

['e x a m p l e ', 'T H I S ']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download