首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从包含关键字列表的re.compile中匹配单个关键字

从包含关键字列表的re.compile中匹配单个关键字
EN

Stack Overflow用户
提问于 2017-05-18 08:11:32
回答 2查看 110关注 0票数 1

我有关键字

代码语言:javascript
复制
cat="AUTHORISATION,FORTHCOMING BOARD MEETINGS,PREVIOUS BOARD MEETINGS,BOARD MEETINGS,BOARD MEETING,MINUTES,BOARD PAPERS,AGENDA,COMMUNITY PROFILES,FORTHCOMING GOVERNOR MEETINGS,PREVIOUS GOVERNOR MEETINGS,GOVERNOR MEETINGS,GOVERNOR MEETING,GOVERNOR,COUNCIL OF GOVERNORS,GOVERNING BODY MEETINGS,COMPARISON,APC SUMMARY OF DECISIONS"

我有一些这样的预处理

代码语言:javascript
复制
cat_list=cat.split(',')
cat_list=filter(None, cat_list)
cat_list=[s.strip() for s in cat_list]
cat_list=[re.sub('\r\n' , ' ', s) for s in cat_list]
cat_list=[re.sub(r'([^\s])\s([^\s])', r'\1+(.)+\2',x) for x in cat_list]
cat_list=[re.sub(r'([a-z][a-z]+)', r'(\1)',a,flags=re.I) for a in cat_list]
regexes_cat=[re.compile((r'(?:%s)' % '|'.join(cat_list)),re.IGNORECASE),]

它为我提供列表中的re.compile表达式以执行re.search,因此处理后的最终正则表达式如下所示

代码语言:javascript
复制
(?:(AUTHORISATION)|(FORTHCOMING)+(.)+(BOARD)+(.)+(MEETINGS)|(PREVIOUS)+(.)+(BOARD)+(.)+(MEETINGS)|(BOARD)+(.)+(MEETINGS)|(BOARD)+(.)+(MEETING)|(MINUTES)|(BOARD)+(.)+(PAPERS)|(AGENDA)|(COMMUNITY)+(.)+(PROFILES)|(FORTHCOMING)+(.)+(GOVERNOR)+(.)+(MEETINGS)|(PREVIOUS)+(.)+(GOVERNOR)+(.)+(MEETINGS)|(GOVERNOR)+(.)+(MEETINGS)|(GOVERNOR)+(.)+(MEETING)|(GOVERNOR)|(COUNCIL)+(.)+(OF)+(.)+(GOVERNORS)|(GOVERNING)+(.)+(BODY)+(.)+(MEETINGS)|(COMPARISON)|(APC)+(.)+(SUMMARY)+(.)+(OF)+(.)+(DECISIONS))

但是如果我打印组(0),我会得到这样的结果。

代码语言:javascript
复制
GOVERNORS-MEETINGS.ASP?P=GOVERNORS%27.COUNCIL.MEETINGS

因此,我搜索并发现,我必须使用使其非贪婪,但我无法获得所需的输出,这应该是

代码语言:javascript
复制
GOVERNORS-MEETINGS

我正在针对网页上的URL和文本执行re.search

代码语言:javascript
复制
http://www.qehkl.nhs.uk/governors-meetings.asp?p=governors%27.council.meetings&s=main&ss=becoming.a.foundation.trust
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-19 08:12:40

我建议的解决办法是基于以下假设:

  • regex匹配应该发生在路径的最后一部分(即文件部分,在任何最终的查询字符串之前)。
  • 查询字符串是可选的。

因此,解决方案是首先使用urlparse解析URL,以便只获得运行正则表达式的字符串,而忽略了查找器。与(.)+不同,只需使用懒散的(.*?)来匹配尽可能少的0+字符:

代码语言:javascript
复制
import re
from urlparse import urlparse

cat="AUTHORISATION,FORTHCOMING BOARD MEETINGS,PREVIOUS BOARD MEETINGS,BOARD MEETINGS,BOARD MEETING,MINUTES,BOARD PAPERS,AGENDA,COMMUNITY PROFILES,FORTHCOMING GOVERNOR MEETINGS,PREVIOUS GOVERNOR MEETINGS,GOVERNOR MEETINGS,GOVERNOR MEETING,GOVERNOR,COUNCIL OF GOVERNORS,GOVERNING BODY MEETINGS,COMPARISON,APC SUMMARY OF DECISIONS"
cat_list=cat.split(',')
cat_list=filter(None, cat_list)
cat_list=[s.strip() for s in cat_list]
cat_list=[re.sub('\r\n' , ' ', s) for s in cat_list]
cat_list=[re.sub(r'([^\s])\s([^\s])', r'\1(.*?)\2',x) for x in cat_list] # Allow anything in between the keywords, but as few as possible
cat_list=[re.sub(r'([a-z][a-z]+)', r'(\1)', a, flags=re.I) for a in cat_list]
regex_cat=re.compile(r"(?:{})".format('|'.join(cat_list)),re.IGNORECASE)
#print(regex_cat.pattern)
urls = "GOVERNORS/GOVERNORS-MEETINGS.ASP?P=GOVERNORS%27.COUNCIL.MEETINGS "
o = urlparse(urls)                       # Parse the URL
last_subpart = o.path.split('/').pop()   # Get the last subpart
m = regex_cat.search(last_subpart)       # Run the regex search
if m:                                    # If there is a match...
    print(m.group())                     # Print or do anything with the value

Python演示

票数 1
EN

Stack Overflow用户

发布于 2017-05-19 06:33:18

试试下面的代码-

代码语言:javascript
复制
cat_list=cat.split(',')
cat_list=filter(None, cat_list)
cat_list=[s.strip() for s in cat_list]
cat_list=[re.sub('\r\n' , ' ', s) for s in cat_list]

#Till now all same, following statements have changes
cat_list=[re.sub(r'([^\s])\s([^\s])', r'\1+.+?\2',x) for x in cat_list]
cat_list=['(%s)'%re.sub(r'([a-z]+)', r'(\1)',a,flags=re.I) for a in cat_list]
regexes_cat=[re.compile((r'(?:%s)' % '|'.join(cat_list)),re.IGNORECASE),]

这是工作的演示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44042109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档