首页 > 解决方案 > 忽略 .com 和 .org 或 .net 的正则表达式解决方案

问题描述

我有以下字符串


str1="Google.com"
str2="yahoo.com"
str3="redcross.org"

我的问题是,忽略 .com、.org 或 .net 的有效正则表达式解决方案是什么

预期产出

Google
yahoo
redcross

标签: pythonregex

解决方案


尝试:

import re # Standard regex module


# The ReGeX
regex = re.compile('([\\.a-zA-Z0-9-]+)(?=\\.[a-z]{3,5})')

# The document to extract websites (suffix excluded) from
doc = """
str1="Google.com"
str2="yahoo.com"
str3="redcross.org"
"""

# Find websites (without the suffix) like so:
found_websites = regex.findall(doc)

# Confirm by printing
print(found_websites)

输出:

['Google', 'yahoo', 'redcross']

功能证明:证明

编辑:我在这里做了一个信息更丰富的网站查找器(不是你想要的,我认为,但可能认为有用)


推荐阅读