开发者

RegEx replace in Python

I'm pretty new to regular expressions. I've worked out the string (?i)\$url\[([0-9])\] to match what I'm looking for. I want a string like "this is $url[2] a string" to be passed through that regex, have 2 passed to a function, which returns a string, and $url[2]开发者_开发知识库 is replaced with that string. How do I do that?


def myrepl(match):
    num = int(match.group(1))
    # Do something here
    return str(num) # this will replace the $url[2]

mystr = "this is $url[2] a string"

print re.sub(r'\$url\[([0-9])\]', myrepl, mystr, flags=re.IGNORECASE)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜