删除URL / Email中的所有空格[关闭]

问题描述 投票:0回答:1

我想删除网址/电子邮件地址中的所有空格。地址是“普通”字符串,如:"Today the weather is fine. Tomorrow, we'll see. More information: www.weather .com or info @weather.com"

我正在寻找一个好的正则表达式(使用Python的re模块),但我的版本无法处理所有情况

re.sub(u'(www)([ .])([a-zA-Z\-]+)([ .])([a-z]+)', '\\1.\\3.\\5')
python regex removing-whitespace
1个回答
0
投票

您对url的表达只需要一点修复。电子邮件的正则表达式也可以从url表达式继承。

>>> #EXPRESSIONS:
>>> url = "(www)+([ .])+([a-zA-Z\-]+)+([ .])+([a-z]+)"
>>> ema = "([a-zA-Z]+)+([ +@]+)+([a-zA-Z\-]+.com)"
>>> 
>>> #IMPORTINGS:
>>> import re
>>> 
>>> #YOUR DATA:
>>> string = "Today the weather is fine. Tomorrow, we'll see. More information: www.weather .com or info @weather.com"
>>> 
>>> #Scraping Data
>>> "".join(re.findall(url,string)[0])
'www.weather.com'
>>> "".join(re.findall(ema,string)[0]).replace(" ","")
'[email protected]'
>>> 
© www.soinside.com 2019 - 2024. All rights reserved.