配置文件中存储正则表达式类型转换问题
在配置中存储正则表达式后,需要将它们转换为正则对象才能进行匹配。配置中只能包含字符串,因此读取后的正则表达式类型也是字符串。
解决方案:
一种方法是使用穷举:
import re pattern_list = ['.*?日历\(.*?\)', 'pattern[, flags]'] for pattern in pattern_list: compiled_re = re.compile(pattern) # 使用 compiled_re 执行匹配操作
另一种方法是动态执行字符串:
import re pattern_dict = ['re.compile(.*?日历\(.*?\))', 're.compile(pattern[, flags]'] for pattern_str in pattern_dict: pattern_obj = eval(pattern_str) # 执行字符串并返回正则对象 # 使用 pattern_obj 执行匹配操作
以上就是配置文件中存储正则表达式如何转换为正则对象?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号