简体   繁体   English

正则表达式匹配/替换

[英]Regular Expression Match/Replace

What would be the equivalent of this (Javascript) in python? 什么是python中的这个(Javascript)的等价物?

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

I'm new at Python and not quite understanding the regex system yet :( 我是Python的新手,还不太了解正则表达式系统:(

You've pretty much already got it 你已经得到了它

>>> x = re.search("sub(\d+)\.domain\.com\/(\d+)","sub123.domain.com/546").groups()
('123', '546')
>>> url = "%s blah blah %s" % x

The rough equivalent of your code in Python would be 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM