python域名的正则表达式 [英] python regular expression for domain names

查看:439
本文介绍了python域名的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下正则表达式从文本中提取域名,但是它什么都不产生,出了什么问题?我不知道这是否适合问这个修复代码的问题,也许我应该阅读更多。我只想节省一些时间。
谢谢

I am trying use the following regular expression to extract domain name from a text, but it just produce nothing, what's wrong with it? I don't know if this is suitable to ask this "fix code" question, maybe I should read more. I just want to save some time. Thanks

pat_url = re.compile(r'''

            (?:https?://)*

            (?:[\w]+[\-\w]+[.])*

            (?P<domain>[\w\-]*[\w.](com|net)([.](cn|jp|us))*[/]*)

            ''')

print re.findall(pat_url,"http://www.google.com/abcde")

我希望输出为google.com

I want the output to be google.com

推荐答案

不要使用正则表达式这个。使用 urlparse 标准库。阅读/维护更简单直观,更容易。

Don't use regex for this. Use the urlparse standard library instead. It's far more straightforward and easier to read/maintain.

http://docs.python.org/library/urlparse.html

这篇关于python域名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆