python - Python regex ignoring pattern
问题描述
I have a list of two keywords like below:
keywords = ["Azure", "Azure cloud"]
but python unable to find the second keyword "Azure cloud"
>>> keywords = ["Azure", "Azure cloud"]
>>> r = re.compile('|'.join([re.escape(w) for w in keywords]), flags=re.I)
>>> word = "Azure and Azure cloud"
>>> r.findall(word)
['Azure', 'Azure']
I am expecting the output like this : ['Azure', 'Azure', 'Azure cloud']
Any guide/help would be highly appreciated!
解决方案
You can run multiple search.
import itertools
import re
keywords = ["Azure", "Azure cloud"]
patterns = [re.compile(re.escape(w), flags=re.I) for w in keywords]
word = "Azure and Azure cloud"
results = list(itertools.chain.from_iterable(
r.findall(word) for r in patterns
))
print(results)
output:
['Azure', 'Azure', 'Azure cloud']
Append
if I'd have word = "Azure and azure cloud" - I will have the output as ['Azure', 'azure', 'azure cloud'] - so the 2nd keyword "azure" which is in small, if i would have to get the exact word matching with the "keywords" list which is "Azure", what modification has to be made in the code?
The flag re.I
means ignore-case. So simply remove this.
patterns = [re.compile(re.escape(w)) for w in keywords]
Append 2
sorry my last comment was vague, so I want the pattern matching to ignore the case, but while fetching the output I would want the keywords to have exact case what we have in the "keyword" list and not in the "word"
Sorry for misunderstanding. Try this:
import re
keywords = ["Azure", "azure cloud"]
patterns = [re.compile(w, flags=re.I) for w in keywords]
word = "Azure and azure cloud"
results = [
match_obj.re.pattern
for r in patterns
for match_obj in r.finditer(word)
]
print(results)
output:
['Azure', 'Azure', 'azure cloud']
I'm not sure it is effecient way, but it works.
Note that I remove re.escape because it cause space escape so result was:
['Azure', 'Azure', 'azure\\ cloud']
推荐阅读
- php - PHP Mailer SMTP 错误:无法连接到服务器:(0)但 SMTP 测试器工作
- javascript - 从 API 调用设置的状态值有时不会显示在页面上(或表单组件上)
- azure - 如何从 AppService 下载 WebJob 可执行文件/脚本
- mesos - 具有单一仲裁的 mesos master
- mysql - sqlx 写:断管
- python-3.x - ModuleNotFoundError:没有名为“pyrlang.gen_server”的模块
- r - r中的条件匹配和返回值
- c++ - C 包装类中的 C++ 常量正确性
- javascript - 如何减少If语句?
- python - 如何修改matplotlib中的图例和背景?