在字符串python Regex上查找域的常规扩展

问题描述 投票:-2回答:3

我想在python中使用正则表达式从一个句子中找到扩展名“COM”。

>>> import re
>>> str = 'finding exstention from string on http://domain.coms/index/page/2'
>>> pattern = re.compile(r'([^\s.\s\:]+\.[^\.\s\:]*)')
>>> 

结果:

domain : 'domain.com'    ### notes: not domain.coms
url : 'http://domain.coms/index/page/2'
python regex
3个回答
0
投票

也许你正在寻找这个:

>>> import re
>>> str = 'finding exstention from string on http://domain.coms/index/page/2'
>>> pattern = re.compile(r'([^\/]*\.(?:com|en|org))')
>>> m = pattern.search(str)
>>> print m.group(1)
domain.com

0
投票
((?:https?:\/\/)?(?:([^\s.\s\:]+\.[^\/]*)(?:\/|$)[^\.\s\:]*))

试试这个.Group 1将是url.Group 2将是domain

见演示。

http://regex101.com/r/sK8oK9/1


0
投票

你可以试试下面的。

>>> s = "finding exstention from string on http://domain.coms/index/page/2"
>>> m = re.search(r'(\S+?([^/.]+\.[^/]+)\S+)', s).group(1)
>>> m
'http://domain.coms/index/page/2'
>>> m = re.search(r'(\S+?([^/.]+\.[^/]+)\S+)', s).group(2)
>>> m
'domain.coms'
© www.soinside.com 2019 - 2024. All rights reserved.