在 python 中用 re.sub 替换特定的命名组
Replace specific named group with re.sub in python
我创建了一个正则表达式来查找像 /places/:state/:city/whatever
这样的网址
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
这很好用:
import re
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
path = '/places/NY/NY/other/stuff'
match = p.match(path)
print match.groupdict()
打印 {'city': 'NY', 'state': 'NY'}
.
如何处理日志文件以将 /places/NY/NY/other/stuff
替换为字符串 "/places/:state/:city/other/stuff"
?我想了解 "cities-type" 有多少个网址,而不关心这些地方具体是 (NY
、NY
)。
简单的方法可能会失败:
import re
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
path = '/places/NY/NY/other/stuff'
match = p.match(path)
if match:
groupdict = match.groupdict()
for k, v in sorted(groupdict.items()):
path = path.replace(v, ':' + k, 1)
print path
将打印 /places/:city/:state/other/stuff
,这是向后的!
感觉应该有一些使用方法re.sub
但是我看不到。
想出了一个更好的方法来做到这一点。编译后的正则表达式上有一个 属性 groupindex
,它在模式字符串中打印组 及其顺序 :
>>> p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
>>> p.groupindex
{'city': 2, 'state': 1}
可以很容易地以正确的顺序迭代:
>>> sorted(p.groupindex.items(), key=lambda x: x[1])
[('state', 1), ('city', 2)]
使用这个,我应该能够保证我以正确的从左到右的顺序替换匹配项:
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
path = '/places/NY/NY/other/stuff'
match = p.match(path)
if match:
groupdict = match.groupdict()
for k, _ in sorted(p.groupindex.items(), key=lambda x: x[1]):
path = path.replace(groupdict[k], ':' + k, 1)
print path
这会以正确的顺序遍历组,确保替换也以正确的顺序发生,从而可靠地产生正确的字符串:
/places/:state/:city/other/stuff
我创建了一个正则表达式来查找像 /places/:state/:city/whatever
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
这很好用:
import re
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
path = '/places/NY/NY/other/stuff'
match = p.match(path)
print match.groupdict()
打印 {'city': 'NY', 'state': 'NY'}
.
如何处理日志文件以将 /places/NY/NY/other/stuff
替换为字符串 "/places/:state/:city/other/stuff"
?我想了解 "cities-type" 有多少个网址,而不关心这些地方具体是 (NY
、NY
)。
简单的方法可能会失败:
import re
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
path = '/places/NY/NY/other/stuff'
match = p.match(path)
if match:
groupdict = match.groupdict()
for k, v in sorted(groupdict.items()):
path = path.replace(v, ':' + k, 1)
print path
将打印 /places/:city/:state/other/stuff
,这是向后的!
感觉应该有一些使用方法re.sub
但是我看不到。
想出了一个更好的方法来做到这一点。编译后的正则表达式上有一个 属性 groupindex
,它在模式字符串中打印组 及其顺序 :
>>> p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
>>> p.groupindex
{'city': 2, 'state': 1}
可以很容易地以正确的顺序迭代:
>>> sorted(p.groupindex.items(), key=lambda x: x[1])
[('state', 1), ('city', 2)]
使用这个,我应该能够保证我以正确的从左到右的顺序替换匹配项:
p = re.compile('^/places/(?P<state>[^/]+)/(?P<city>[^/]+).*$')
path = '/places/NY/NY/other/stuff'
match = p.match(path)
if match:
groupdict = match.groupdict()
for k, _ in sorted(p.groupindex.items(), key=lambda x: x[1]):
path = path.replace(groupdict[k], ':' + k, 1)
print path
这会以正确的顺序遍历组,确保替换也以正确的顺序发生,从而可靠地产生正确的字符串:
/places/:state/:city/other/stuff