Reputation: 97
how can I write a regex to remove the words coming after semicolon until it gets to the first period mark?
for instance:
try to be nice; underastand people. start fresh.
I want to remove "understand people" and have something like
try to be nice. start fresh
this is my regex but sadly it does not work:
sentence = ''.join(re.sub(r';.*?.', '.', sentence))
Thanks!
Upvotes: 2
Views: 1491
Reputation: 626920
You forgot to escape the dot: r';.*?\.'
. Also, there is no need to join
anything here, you pass a string to re.sub
and you get a string after.
A r';[^.]+'
regex seems better though as it will match a ;
and 1+ chars other than a literal dot (so, you can replace with an empty string):
import re
sentence = "try to be nice; underastand people. start fresh."
sentence = re.sub(r';[^.]+', '', sentence)
print(sentence)
# => try to be nice. start fresh.
See the Python demo
Upvotes: 3