假设我有这些网址。
http://abdd.eesfea.domainname.com/b/33tA$/0021/file
http://mail.domainname.org/abc/abc/aaa
http://domainname.edu
我只想把“domainame.com”或“domainname.org”或“domainname.edu”提取出来。 我该怎么做?
我想,我需要在“com | org | edu…”之前找到最后一个“点”,并打印出从这个“点”的上一个点到下一个点(如果有的话)的内容。
需要关于常规表达式的帮助。 谢谢!!! 我正在使用Python。
如果你想走regex路线。。。
RFC-3986是有关uri的权威。Appendix B提供了此regex以将一个regex分解为其组件:
这是一个增强的、Python友好的版本,它利用了命名的捕获组。它以工作脚本中的函数表示:
有关根据RFC-3986分离和验证URI的更多信息,您可能想看一看我一直在写的一篇文章:Regular Expression URI Validation
为什么使用regex?
http://docs.python.org/library/urlparse.html
除了贾斯的回答。 如果您不想使用urlparse,只需拆分URL
协议条(http://or https://) 您刚刚按第一个出现的“/”拆分了字符串。这会给你留下这样的东西: 第二个URL上的“mail.domainname.org”。然后可以按“.”拆分,只需按[-2]从列表中选择最后两个
这将始终产生domainname.org或其他内容。如果你把协议去掉了,并且URL是有效的。
我只想使用urlparse,但这是可以做到的。 不知道雷鬼的事,但我会这么做的。
相关问题 更多 >
编程相关推荐