0

python中这个(Javascript)的等价物是什么?

var patt = /sub(\d+)\.domain\.com\/(\d+)/
  , m    = url.match(patt)
  , url = 'http://sub' + m[1] + '.domain.com/' + m[2]

我是 Python 新手,还不太了解正则表达式系统:(

4

2 回答 2

1

你已经得到了它

>>> x = re.search("sub(\d+)\.domain\.com\/(\d+)","sub123.domain.com/546").groups()
('123', '546')
>>> url = "%s blah blah %s" % x
于 2012-05-07T22:16:39.627 回答
1

您在 Python 中的代码大致相当于

import re

url = 'http://sub36.domain.com/54'

patt = re.compile("sub(\d+)\.domain\.com\/(\d+)")
m = patt.search(url)
url = 'http://sub'+m.group(1)+'.domain.com/'+m.group(2)
于 2012-05-07T22:19:52.923 回答