用于忽略.com和.org或.net的正则表达式解决方案

2024-10-03 23:23:37 发布

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

我有以下字符串


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

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

预期产量

Google
yahoo
redcross

Tags: 字符串orgcomnetgoogle解决方案yahoo产量
2条回答

在python中,您可以这样做

import re
str_list = re.findall(r"\w*", string_input)
output = str_list[0]

尝试:

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']

功能性证明:proof

编辑:我制作了一个信息更丰富的网站查找工具(我认为这不是你想要的,但可能认为有用)here

相关问题 更多 >