Home > Mobile >  remove . from a sentences using regular expression
remove . from a sentences using regular expression

Time:06-17

I have sentence ex :

input = "you will get 2.55 percent discount. it will be good."
input = re.sub(r"[a-z][/.]",repl="",string=input)
print(input)

output getting : you will get 2.55 percent discoun it will be goo

expected_output = `you will get 2.55 percent discount it will be good'

CodePudding user response:

You can use rstrip as follows,

input = "you will get 2.55 percent discount."
input = input.rstrip('.')

CodePudding user response:

Remove [a-z] from your code because it can involve in to replace your character t

import re
input = "you will get 2.55 percent discount."
input = re.sub(r"[/.]",repl="",string=input)

print(input)

CodePudding user response:

Try something like this:

outp = re.sub(r'([a-z])\.', r'\1', inp)

It replaces a letter a-z followed by a dot by that letter, effectively removing the dot. Adjust what can appear before the dot to trigger the removal.

  • Related