regex - 使用 re.compile 编译列表中的多个字符串
问题描述
我想编制这份清单
list_serv = ["no","none","nothing","never"]
但我无法做到这一点,所以我必须单独键入该列表的每个元素,如下所示
compiled_list_serv = re.compile(r'no\b|not\b|none\b|nothing\b')
有人可以建议我使用 Python 编写一个高效的代码,以便我可以一次性编译列表“list_serv”中的所有元素吗?
生成的编译应如下所示:
re.compile(r'no\b|not\b|none\b|nothing\b', re.UNICODE)
解决方案
你想达到什么目的?
从给定的代码中,您可以这样做:
re.compile("|".join(list_serv), re.UNICODE)
推荐阅读
- r - 未更改任何内容后未加载文件
- c# - FluentValidation 和自定义消息,告诉用户允许/预期哪些值
- python - 我可以将 Slack 事件 API 与斜杠命令一起使用吗?
- c - 重置日志文件后双重释放或损坏(顶部)
- c# - 为什么 IE 的 selenium webdriver 无法单击具有特定 CSS 的 iframe 中的元素?
- javascript - 在数据中使用特殊字符时,Json 解析会出错
- regex - 正则表达式 - 贪婪直到下一场比赛
- r - 如果模式匹配,则为列名添加前缀
- c++ - 将 C++ DLL 导出到 Visual Basic
- json - extract subset of imported json file in vue's data() function