Angad Pal Singh
Angad Pal Singh

Reputation: 43

from text deleting specific characters using re.sub

m ='. Kansas City has a population of 475378. Local time in Kansas City is CST.'
d = re.sub('..*?.', '', m)
print(d)

I am trying to delete the line between the two periods.It gives me the same output How can i correct it?

Upvotes: 1

Views: 31

Answers (1)

sniperd
sniperd

Reputation: 5274

You need to escape the dots with a backslash, they mean any character. You are also better off using a negative character class instead of .*? as it is doing what you are really asking, 'all/any non dots':

[^.]*

Here is an example of what you are asking for.

import re

m ='. Kansas City has a population of 475378. Local time in Kansas City is CST.'
d = re.sub('\.[^.]*\.', '', m)
print(d)

If you want to keep the periods you can simply do this:

d = re.sub('\.[^.]*\.', '..', m)

http://regex101.com/ is a really good site to play around with your regex to tweak it exactly how you'd like to have it work. Good luck!

Upvotes: 1

Related Questions