这是模式
pattern_strings = ['\\xc2d', '\\xa0', '\\xe7', '\\xc3\\ufffdd', '\\xc2\\xa0', '\\xc3\\xa7', '\\xa0\\xa0', '\\xc2', '\\xe9']
join_pattern = '[' + '|'.join(pattern_strings) + ']'
pattern = re.compile(join_pattern)
这是功能
def find_pattern(path):
with open(path, 'r') as f:
for line in f:
# print line
found = pattern.search(line)
if found:
print dir(found)
logging.info('found in line - ' + line)
logging.info('found - ' + str(found.group(0)))
这是输入
\xc2d
d\xa0
\xe7
\xc3\ufffdd
\xc3\ufffdd
\xc2\xa0
\xc3\xa7
\xa0\xa0
'619d813\xa03697'
当我运行它时,我得到的输出为
INFO:root:found in line - \xc2d
INFO:root:found - d
INFO:root:found in line - d\xa0
INFO:root:found - d
INFO:root:found in line - \xc3\ufffdd
INFO:root:found - u
INFO:root:found in line - \xc3\ufffdd
INFO:root:found - u
INFO:root:found in line - '619d813\xa03697'
INFO:root:found - d
问题
- 为什么它不告诉整个模式\xc2d
?我在这里做错了什么吗?- 我需要做什么才能让模式匹配\xc2d
而不是d
更新
chaging tojoin_pattern = '(' + '|'.join(pattern_strings) + ')'
不匹配任何东西
更新 1
pattern_strings = ['\\xc2d', '\\xa0', '\\xe7', '\\xc3\\ufffdd', '\\xc2\\xa0', '\\xc3\\xa7', '\\xa0\\xa0', '\\xc2', '\\xe9']
join_pattern = '|'.join(pattern_strings)
pattern = re.compile(join_pattern)
这与输入中的任何内容都不匹配:(