[英]Using variables in re.findall() regex function
I have a list of regex patterns like k[az]p[az]+a and a list of words that can fit into these patterns. 我有一个正则表达式模式列表,例如k [az] p [az] + a,以及一个适合这些模式的单词列表。 Now, the problem is that, when I use:
现在的问题是,当我使用时:
re.findall(r'k[a-z]p[a-z]+a', list)
Everything works properly, but when I replace the raw expression with a variable like: 一切正常,但是当我用一个变量替换原始表达式时:
pattern = "r'" + pattern + "'"
and then try: 然后尝试:
re.findall(pattern, list)
or 要么
re.findall(str(pattern), list)
It no longer works. 它不再起作用。 How could I fix it?
我该如何解决?
Thanks! 谢谢! Spike
穗
You are overthinking it. 您想得太多了。 The
r
prefix is not part of the pattern string itself, it merely indicates that the following string should not use escape codes for certain characters. r
前缀不是模式字符串本身的一部分,它仅指示以下字符串不应对某些字符使用转义码。
This will work without adjusting your pattern: 这将在不调整模式的情况下起作用:
re.findall(pattern, list)
If your pattern
contains characters that do not need escaping (as they do not), you can add the prefix r
to the pattern definition. 如果您的
pattern
包含不需要转义的字符(因为它们不需要转义),则可以在模式定义中添加前缀r
。 Suppose you want to search for a different regex, then use 假设您要搜索其他正则表达式,然后使用
pattern = r'k\wp\wa'
re.findall(pattern, list)
and you don't need to escape it. 而且您不必逃避它。 Since
pattern
in itself is a perfectly ordinary string, you can concatenate it with other strings: 由于
pattern
本身是一个非常普通的字符串,因此可以将其与其他字符串连接:
start = 'a'
middle = 'b'
end = 'c'
pattern = a + r'\w' + b + r'\w' + c
re.findall(pattern, list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.