删除URL/Emai中的所有空白

2024-03-29 11:57:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在寻找一个好的regex(使用Python的re模块),但是我的版本不能处理所有的情况

re.sub(u'(www)([ .])([a-zA-Z\-]+)([ .])([a-z]+)', '\\1.\\3.\\5')

Tags: the字符串recomurltodayis电子邮件
1条回答
网友
1楼 · 发布于 2024-03-29 11:57:44

你的url表达式只需要稍微修改一下。电子邮件的regex表达式也可以从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(" ","")
'info@weather.com'
>>> 

相关问题 更多 >