pattern = "world! {} "
text = "hello world! this is python"
鉴于上面的模式和文本,我如何生成一个将模式作为第一个参数,将文本作为第二个参数并输出单词“ this”的函数?
例如
find_variable(pattern, text)
==>返回“ this”,因为“ this”
答案 0 :(得分:1)
您可以使用此函数,该函数使用string.format
来构建具有单个捕获组的正则表达式:
>>> pattern = "world! {} "
>>> text = "hello world! this is python"
>>> def find_variable(pattern, text):
... return re.findall(pattern.format(r'(\S+)'), text)[0]
...
>>> print (find_variable(pattern, text))
this
PS:您可能想在函数内部添加一些完整性检查,以验证字符串格式和成功的findall
。
答案 1 :(得分:0)
不是一个像anubhava那样的内衬,而是使用了基本的python知识:
const encrypted = ... //holds our encrypted data
const key = ... // holds our 32 bytes key
const iv = ... //holds our 32 bytes iv
const decipher = new Rijndael(key, 'cbc');
const decryptedPadded = decipher.decrypt(encrypted, 256, iv);
//Remember to un-pad result
const decrypted = padder.unpad(decryptedPadded, 32);
const clearText = decrypted.toString('utf8');
console.log(clearText); //-> Here is my plain text