python - How to check that the two consecutive words have the same regex pattern -
i've tried several hours couldn't reach goal.
here string:'hello world, way stackoverflow cool place'. i'm looking match 2 consecutive words have same regex pattern.
for example want replace words consecutive , start capital letter string "xx".
so when applied string result should be:
hello world,xx xx xx stackoverflow cool place
here snippet:
mystring='hello world,by way stackoverflow cool place' re.sub(r"[a-z]\w+","xx",mystring) but i'm getting is: 'xx world,xx xx xx stackoverflow cool place'
using regex module:
>>> import regex >>> text = 'hello world,by way stackoverflow cool place' >>> regex.sub(r'\b[a-z]\w+(?=\s+[a-z]\w+)|(?<=\b[a-z]\w+\s+)[a-z]\w+', 'xx', text) 'hello world,xx xx xx stackoverflow cool place'
Comments
Post a Comment