Home > OS >  Remove dot following specific text
Remove dot following specific text

Time:02-21

I'm trying to use remove dot (.) from specific following words like com and org for text cleaning using Python e.g.

Input: cnnindonesia.com liputan.org

Output: cnnindonesiacom liputanorg

Anybody has an idea using regex or iterations? Thank you.

CodePudding user response:

You can use .replace() and a list comprehension; regular expressions aren't necessary here:

data = ["cnnindonesia.com", "liputan.org"]

print([url.replace(".com", "com").replace(".org", "org") for url in data])

CodePudding user response:

Try this

input = "cnnindonesia.com liputan.org"

output = input.replace(".", "")

print(output)

Output

cnnindonesiacom liputanorg

CodePudding user response:

You can split on the '.' and then join it.

input = "cnnindonesia.com liputan.org"

output = input.split(".")

output = ("").join(output)

CodePudding user response:

If you have multiple patterns, re would be useful:

import re

s = "cnnindonesia.com liputan.org example.net twitch.tv"

output = re.sub(r"\.(com|org|net|tv)", r"\1", s)
print(output) # cnnindonesiacom liputanorg examplenet twitchtv
  • Related