在re.findall()regex函数中使用变量

时间:2023-01-25 22:33:23

I have a list of regex patterns like k[a-z]p[a-z]+a and a list of words that can fit into these patterns. Now, the problem is that, when I use:

我有一个正则表达式列表,如k [a-z] p [a-z] + a和一个可以适合这些模式的单词列表。现在,问题在于,当我使用时:

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

1 个解决方案

#1


0  

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. Suppose you want to search for a different regex, then use

如果您的模式包含不需要转义的字符(因为它们不包含),则可以将前缀r添加到模式定义中。假设您要搜索不同的正则表达式,然后使用

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:

而且你不需要逃避它。由于模式本身是一个完全普通的字符串,您可以将它与其他字符串连接起来:

start = 'a'
middle = 'b'
end = 'c'
pattern = a + r'\w' + b + r'\w' + c
re.findall(pattern, list)

#1


0  

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. Suppose you want to search for a different regex, then use

如果您的模式包含不需要转义的字符(因为它们不包含),则可以将前缀r添加到模式定义中。假设您要搜索不同的正则表达式,然后使用

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:

而且你不需要逃避它。由于模式本身是一个完全普通的字符串,您可以将它与其他字符串连接起来:

start = 'a'
middle = 'b'
end = 'c'
pattern = a + r'\w' + b + r'\w' + c
re.findall(pattern, list)