2012-05-07 175 views
0

python中的這個(Javascript)等價於什麼?正則表達式匹配/替換

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

我是新的Python和不甚瞭解的正則表達式系統尚未:(

+0

是javascript? –

+0

@MikePennington啊,是的...抱歉忘了補充一點! –

回答

1

你已經差不多已經得到它

>>> x = re.search("sub(\d+)\.domain\.com\/(\d+)","sub123.domain.com/546").groups() 
('123', '546') 
>>> url = "%s blah blah %s" % x 
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)