Python Regex sub on single character -
is there way perform sub on 1 character of matching pattern?
for example, if have string
"this word. word. h.e. stein"
and want perform sub on '.'s @ end of sentence becomes
"this word word h.e. stein"
how should go doing this?
you don't need use regular expression:
>>> "qwerty.".replace('.', '', 1) # 1 -> replace count (only once) 'qwerty'
to delete last character, use slice:
>>> "qwerty."[:-1] 'qwerty'
update according question edit.
>>> text = "this word. word. h.e. stein" >>> re.sub(r'(\w{2})\.', r'\1', text) 'this word word h.e. stein'
(\w{2})\.
: match period after 2 word characters. capture word characters group 1. later referenced \1
.
Comments
Post a Comment