I have a string containing multiple lines with words, and I want to extract all words from the string except those that start with c.
I tried this
import re
inp='''bat
cat
mat
'''
pt=re.compile(r'[^c][a-z]+')
ma=pt.findall(inp)
I’m getting
['bat', '\ncat', '\nmat']
This works perfectly fine if I explicitly mention ‘at’
pt=re.compile(r'[^c]at')
Output for this is:
['bat', 'mat']
But it doesn’t work using pt=re.compile(r'[^c][a-z]+')
>Solution :
import re
inp='''bat
cat
map
'''
pt=re.compile(r'\b[^\nc][a-z]+')
ma=pt.findall(inp)
print(ma)
Output:
['bat', 'map']