正则表达式,它将给出特定的单词python

2024-04-25 22:37:12 发布

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

我正在寻找一个正则表达式,它给出以字母、数字或下划线开头的单词。它只能在单词之间包含点('.'),而不能在末尾包含点('.'),并且应删除所有其他特殊字符。 e、 g

WARC-_Target-URI: http://www.allchocolate.com/health/basics/

应该给

WARC,_Target,URI,http,www.allchocolate.com,健康,基础

任何形式的帮助都将不胜感激


Tags: comhttptargetwww字母数字uri单词
3条回答

给你:

from re import findall

print findall(r'\w[\w.]*\w', 'WARC-_Target-URI: http://www.allchocolate.com/health/basics/')

['WARC', '_Target', 'URI', 'http', 'www.allchocolate.com', 'health', 'basics']

与其他解决方案不同,这将适用于任何情况(不仅仅是您发布的示例)

import re
test = "WARC-_Target-URI: http://www.allchocolate.com/health/basics/"
print re.findall(r"[\w'.]+", test)
s = 'WARC-_Target-URI: http://www.allchocolate.com/health/basics/'

parts = [x for x in re.split(r'[/:-]',s) if x]

print(parts)

['WARC', '_Target', 'URI', ' http', 'www.allchocolate.com', 'health', 'basics']

相关问题 更多 >

    热门问题