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

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -