import re
A = 'Thsi is my [site](http://example.com/url) you can watch it.'
site = re.compile( "\[(.*)\]" ).search(A).group(1)
url = re.compile( "\((.*)\)" ).search(A).group(1)
print A.replace("[{0}]".format(site), "").replace("({0})".format(url), '<a href="{0}">{1}</a>'.format(url, site))
输出:
^{pr2}$
根据请求在评论中更新:
s = 'my [site](site.com) is about programing (python language)'
site, url = s[s.find("[")+1:s.find(")")].split("](")
print s.replace("[{0}]".format(site), "").replace("({0})".format(url), '<a href="{0}">{1}</a>'.format(url, site))
输出:
my <a href="site.com">site</a> is about programing (python language)
这应该有帮助
输出:
^{pr2}$根据请求在评论中更新:
输出:
我不是一个降价专家,但如果这确实是您试图替换的markdown,而不是您自己的语法,那么您应该使用适当的解析器。请注意,如果您将字符串直接粘贴到stackoverflow中(它也使用markdown),它将被转换为一个链接,因此它显然是有效的markdown。在
但是,如果它确实是您自己的格式,请尝试下面的转换
进入
^{pr2}$使用以下匹配项:
以及以下替换regex:
在python中,
re.sub(match, replace, stringThatYouWantToReplaceStuffIn)
应该能做到这一点。不要忘记将re.sub
的返回值赋给任何应该包含新字符串的变量。在相关问题 更多 >
编程相关推荐