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

标签 python regex string

我正在从 .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/

相关文章:

python - 执行 re.search() 直到行尾,然后更新数据帧

mysql - 如何在 MySQL 中匹配恰好有 2 个斜杠的字符串

r - R 中字符串的 tidyr fill() 是否有等价物?

R:从字符串转换为 double

javascript - 使用javascript从字符串中解析youtube视频id

C++ c2664 错误 "cannot convert argument 1 from std::string to _Elem *"

python - 如何获得每个时期而不是每个批处理的损失?

python - 在 Jupyter - Python 中有 2 个 Ipywidgets 作用于一个 matplotlib 图

python - 在 Python 中计算稀疏矩阵的 N 个最小特征值

javascript - 如何在 javascript 中用多个逗号和冒号拆分字符串?