简体   繁体   中英

Python re.sub replace match with it's md5sum

I'm trying to parse such a string:

<@Something> there is some regular text <@something_else> and even more <@foo> <@bar> text

And replace all <@tokens> with theirs md5 sums.

Is it possible with python re.sub? How can I pass @token to a function and the function's output to re.sub ?

So far I've tried the simplest approach with:

import re

def fun(str):
    return str.replace('@', '!')

pattern = r'(<@\w+>)'
string = '<@AAAA> some text and more text <@BBBBB>'

print fun('<@AAAA>')
print string
print re.sub(pattern, fun(r'\1'), string)

Without success. The fun function works as expected outside re.sub(), but not within it.

是的,如文档所示, re.sub()的“ replacement”参数可以是接受匹配对象并返回替换值的函数。

With hashlib library:

import hashlib, re

s = '<@Something> there is some regular text <@something_else> and even more <@foo> <@bar> text'
result = re.sub(r'<@[^>]+>', lambda m: hashlib.md5(m.group().encode()).hexdigest(), s)

print(result)

The output:

eb6eae14fb79abc1339b7096ae00a5e9 there is some regular text 16960eadb21d27a1b52e5c71a5ae7357 and even more 281dc7c0420f8e6ef66e58ecb979d087 31c0c5b91906d520a4dec601241833a6 text

https://docs.python.org/3/library/hashlib.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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