python中两个单词之间的正则表达式

2024-10-06 09:13:34 发布

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

我在努力获得价值

l1 = [u'/worldcup/archive/southafrica2010/index.html', u'/worldcup/archive/germany2006/index.html', u'/worldcup/archive/edition=4395/index.html', u'/worldcup/archive/edition=1013/index.html', u'/worldcup/archive/edition=84/index.html', u'/worldcup/archive/edition=76/index.html', u'/worldcup/archive/edition=68/index.html', u'/worldcup/archive/edition=59/index.html', u'/worldcup/archive/edition=50/index.html', u'/worldcup/archive/edition=39/index.html', u'/worldcup/archive/edition=32/index.html', u'/worldcup/archive/edition=26/index.html', u'/worldcup/archive/edition=21/index.html', u'/worldcup/archive/edition=15/index.html', u'/worldcup/archive/edition=9/index.html', u'/worldcup/archive/edition=7/index.html', u'/worldcup/archive/edition=5/index.html', u'/worldcup/archive/edition=3/index.html', u'/worldcup/archive/edition=1/index.html']

我试着从下面这样的东西开始做正则表达式

m = re.search(r"\d+", l)
print m.group()

但我希望值介于“archive/”和/索引.html“
我目不转睛地看了看,试过(?<=archive/\/index.html).*(?=\/index.html:)

但这对我没用。。我怎样才能得到我的结果列表

result = ['germany2006','edition=4395','edition=1013' , ...]

Tags: rel1列表searchindexhtmlgroupresult
3条回答

如果你确定模式总是匹配的,你可以使用这个

import re
print [re.search("archive/(.*?)/index.html", l).group(1) for l in l1]

或者你可以这样分开

print [l.rsplit("/", 2)[-2] for l in l1]

正则表达式

m = re.search(r'(?<=archive\/).+(?=\/index.html)', s)

可以解决这个问题,假设s是列表中的一个字符串。你知道吗

你需要四处看看。你需要这样使用它:

>>> [re.search(r"(?<=archive/).*?(?=/index.html)", s).group() for s in l1]
[u'southafrica2010', u'germany2006', u'edition=4395', u'edition=1013', u'edition=84', u'edition=76', u'edition=68', u'edition=59', u'edition=50', u'edition=39', u'edition=32', u'edition=26', u'edition=21', u'edition=15', u'edition=9', u'edition=7', u'edition=5', u'edition=3', u'edition=1']

相关问题 更多 >