regex - Python substitute a word for a word and the next concatenated -
i want able take in string , if r'\snot\s' located, concatenate 'not' , next word (replacing white space in between underscore).
so if string
string="not name brian , not happy nothing"
the result after regular expression be:
'not_that name brian , not_happy nothing'
(not in nothing not touched).
i need locate 'not' either seperated white space or @ start of sentence , join '_' , next word.
use re.sub() saving groups:
>>> re.sub(r"not\s\b(.*?)\b", r"not_\1", string) 'not_that name brian , not_happy nothing' not\s\b(.*?)\b here match not followed space, followed word (\b word boundaries). (.*?) capturing group capture word after not can reference in substitution (\1).
Comments
Post a Comment