gpt4 book ai didi

python - 如何读取字符串、转换为正则表达式并编译它?

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:04 27 4
gpt4 key购买 nike

我正在从 .tsv 文件中读取信息,获取每行中表示正则表达式的字符串。例如,我想检测“remix”或“re-mix”,因此我读取 '\bre-?mix\b' 并必须将其转换。我搜索了一下,发现this question along the same lines ,但我已经测试了答案,但没有一个对我有用。

当我在模式上使用 re.escape() 时,它最终会像这样:'\bre-\?mix\b',并且在使用 re.compile() 并在 "上执行 re.search() 之后重新混合”,它失败了。我尝试简单地将 raw_regex.replace('\\b', '\\\\b') 输入到 re.compile() 中,并检查模式,它看起来像应该的那样,但仍然没有捕获简单的 if Compiled_regex.search ("remix") 检查。

我在这里做错了什么?我需要做的就是读取原始文本正则表达式,转换并编译它们。如果需要在输入端进行某些更改,也可以这样做。谢谢!

最佳答案

该程序读取一个字符串,将其编译为正则表达式,并针对'remix'对其进行测试。不需要“转换”步骤:

#!/usr/bin/python2.7
import csv
import re
with open('x.tsv') as input_file:
input_file = csv.reader(input_file, delimiter='\t')
for row in input_file:
compiled_regex = re.compile(row[0])
print row[0], bool(compiled_regex.search('remix')), bool(compiled_regex.search('re-mix'))

输入:

remix
re-?mix
\bre-?mix\b
.*
this line should not match

输出:

remix True False
re-?mix True True
\bre-?mix\b True True
.* True True
this line should not match False False

关于python - 如何读取字符串、转换为正则表达式并编译它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25732446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com