小编Ete*_*ity的帖子

str.startswith,带有要测试的字符串列表

我试图避免使用如此多的if语句和比较,只是使用一个列表,但不知道如何使用它str.startswith:

if link.lower().startswith("js/") or link.lower().startswith("catalog/") or link.lower().startswith("script/") or link.lower().startswith("scripts/") or link.lower().startswith("katalog/"):
    # then "do something"
Run Code Online (Sandbox Code Playgroud)

我希望它是:

if link.lower().startswith() in ["js","catalog","script","scripts","katalog"]:
    # then "do something"
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

python string list

154
推荐指数
2
解决办法
11万
查看次数

使用正则表达式提取URL链接重新匹配字符串 - Python

我一直在尝试使用re api从文本文件中提取URL.任何以http://,https://和www开头的链接.

该文件包含文本以及html源代码,html部分很容易,因为我可以使用BeautifulSoup提取它们,但普通文本似乎更具挑战性.我在网上发现这似乎是URL提取的最佳实现,但它在某些标签上失败,特别是它无法处理标签并将它们包含在URL中.感谢任何帮助,因为我自己并不熟悉字符串匹配

这是签名

sp1=re.findall("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", str(STRING))
sp2=re.findall('www.(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str(STRING))
Run Code Online (Sandbox Code Playgroud)

例子:

http://www.website.com/science/</span></a><o:p></o:p></span></div><div
www.website.com/library/</span></a></span></i><span
http://awebsite.com/Groups</a><div>
Run Code Online (Sandbox Code Playgroud)

python string url extraction matching

2
推荐指数
1
解决办法
6928
查看次数

标签 统计

python ×2

string ×2

extraction ×1

list ×1

matching ×1

url ×1