[英]Using Regex Plus Function in Python to Encode and Substitute
I'm trying to substitute something in a string in python and am having some trouble. 我试图用python中的字符串替换某些内容,但遇到了一些麻烦。 Here's what I'd like to do. 这就是我想要做的。
For a given comment in my posting: 对于我的帖子中给定的评论:
"here are some great sites that i will do cool things with! https://stackoverflow.com/it's a pig & http://google.com"
I'd like to use python to make the strings like this: 我想使用python来制作这样的字符串:
"here are some great sites that i will do cool things with! <a href="http://stackoverflow.com">http%3A//stackoverflow.com</a> & <a href="http://google.com">http%3A//google.com</a>
Here's what I have so far... 到目前为止,这就是我所拥有的...
import re
import urllib
def getExpandedURL(url)
encoded_url = urllib.quote(url)
return "<a href=\"<a href="+url+"\">"+encoded_url+"</a>"
text = '<text from above>'
url_pattern = re.compile('(http.+?[^ ]+', re.I | re.S | re.M)
url_iterator = url_pattern.finditer(text)
for matched_url in url_iterator:
getExpandedURL(matched_url.groups(1)[0])
But this is where i'm stuck. 但是,这就是我卡住的地方。 I've previously seen things on here like this: Regular Expressions but for Writing in the Match but surely there's got to be a better way than iterating through each match and doing a position replace on them. 我以前在这里看到过这样的事情: 正则表达式,但要在比赛中写作,但是肯定有比遍历每个比赛并在其上进行位置替换的更好的方法。 The difficulty here is that it's not a straight replace, but I need to do something specific with each match before replacing it. 这里的困难在于它不是直接替换,但是我需要在替换每个匹配项之前做一些特定的事情。
I think you want url_pattern.sub(getExpandedURL, text)
. 我认为您想要url_pattern.sub(getExpandedURL, text)
。
re.sub(pattern, repl, string, count=0) re.sub(pattern,repl,string,count = 0)
Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. 返回通过用替换repl替换字符串中模式中最左边的非重叠出现而获得的字符串。 repl can be either a string or a callable; repl可以是字符串,也可以是可调用的; if a callable, it's passed the match object and must return a replacement string to be used. 如果是可调用的,则将其传递给match对象,并且必须返回要使用的替换字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.