Skip to main content
 首页 » 编程设计

python之如何从文件加载多个正则表达式模式并匹配给定的字符串

2024年11月24日24freeliver54

<分区>

根据提供的代码(针对这篇文章进行了简化),有人可以帮助展示我如何获取正则表达式模式的列表(如果“列表”是要使用的正确类型)以从文本文件加载并匹配到一个字符串?

有许多从文件中加载文本/文本字符串并匹配正则表达式模式的示例,但反之则不然——许多正则表达式模式匹配一​​个文本字符串。

如果我手动创建列表并运行 re.compile,您可能会在代码中看到,我可以使用模式列表来匹配字符串。然而,从文件加载时,re.compile 适合什么位置?

import regex as re 
 
fname = 'regex_strings_short.txt' 
 
string_to_match = 'onload=alert' 
 
# Create a manual list of regexes 
manual_regexes = [ 
    re.compile(r'(?i)\bHP\b(?:[^.,;]{1,20}?)\bnumber\b'), 
    re.compile(r'(?i)\bgmail\b(?:[^.,;]{1,20}?)\bnumber\b'), 
    re.compile(r'(?i)\bearthlink\b(?:[^.,;]{1,20}?)\bnumber\b '), 
    re.compile(r'(?i)onload=alert') 
] 
 
# Create a text file with these five example patterns 
''' 
(?i)\bHP\b(?:[^.,;]{1,20}?)\bnumber\b 
(?i)\bgmail\b(?:[^.,;]{1,20}?)\bnumber\b 
(?i)\bearthlink\b(?:[^.,;]{1,20}?)\bnumber\b 
(?i)onload=alert 
(?i)hello 
''' 
 
# Import a list of regex patterns from the created file 
with open(fname, 'r') as file: 
    imported_regexes = file.readlines() 
 
# Notice the difference in the formatting of the manual list with 'regex.Regex' and 'flags=regex.I | regex.V0' wrapping each item 
print(manual_regexes) 
print('---') 
print(imported_regexes) 
 
# A match is found in the manual list, but no match found in the imported list 
if re.match(imported_regexes[3], my_string): 
    print('Match found in imported_regexes.') 
else: 
    print('No match in imported_regexes.') 
 
print('---') 
 
if re.match(manual_regexes[3], my_string): 
    print('Match found in manual_regexes.') 
else: 
    print('No match in manual_regexes.') 

imported_regexes 没有匹配项,但 manual_regexes 有匹配项。

更新:下面的代码是对我有用的最终解决方案。发布它,因为它可能会帮助有人登陆这里并需要解决方案。

# You must use regex as re and not just 'import re' as \p{} is not correctly escaped 
 
import regex as re 
 
 
 
# Add the post/string to match below 
 
my_string = '<p>HP Support number</p>' 
 
 
 
fname = 'regex_strings.txt' 
 
 
 
# Contents of text file similar to the below 
 
# but without the leading # space - that's only because it's an inline comment here 
 
# (?i)\bHP\b(?:[^.,;]{1,20}?)\bnumber\b 
 
# (?i)\bgmail\b(?:[^.,;]{1,20}?)\bnumber\b 
 
# (?i)】\b(?:[^.,;]{1,1000}?)\p{Lo} 
 
 
 
# Import a list of regex patterns from a file 
 
with open(fname, 'r', encoding="utf8") as f: 
 
    loaded_patterns = f.read().splitlines() 
 
 
 
# print(loaded_patterns) 
 
print(len(loaded_patterns)) 
 
 
 
found = 0 
 
for index, pattern in enumerate (loaded_patterns): 
 
    if re.findall(loaded_patterns[index],my_string): 
 
        print('Match found. ' + loaded_patterns[index]) 
 
        found = 1 
 
 
 
if found == 0: 
 
    print('No matching regex found.')