Python正则表达式匹配一段英文中包含关键字的句子
|
1.问题/需求
在含有多行文字的英文段落或一篇英文中查找匹配含有关键字的句子。
例如在以下字符串:
- text = '''Today I registered my personal blog in the cnblogs and wrote my first essay.
- The main problem of this essay is to use python regular expression matching to filter out
- sentences containing keywords in the paper. To solve this problem, I made many attempts
- and finally found a regular expression matching method that could meet the requirements
- through testing. So I've documented the problem and the solution in this blog post and
- shared it for reference to others who are having the same problem. At the same time,
- this text is also used to test the feasibility of this matching approach. Some additional
- related thoughts and ideas will be added to this blog later.'''
复制代码 中匹配含有’blog‘的句子。
2.解决方法
因为要找出所有含有关键字的句子,所以这里采用re库中findall()方法。同时,由于所搜索的字符串中含有换行符'\n',因此向re.compilel()传入re.DOTALL参数,以使'.'字符能够匹配所有字符,包括换行符'\n'。这样我们匹配创建Pattern对象为:- newre = re.compile('[A-Z][^.]*blog[^.]*[.]', re.DOTALL)<br>newre.findall(text) # 进行匹配<br># 结果为:<br>['Today I registered my personal blog in the cnblogs and wrote my first essay.',<br>"So I've documented the problem and the solution in this blog post and \nshared it for reference to others who are having the same problem.",<br><em id="__mceDel">'Some additional \nrelated thoughts and ideas will be added to this blog later.'] # 这其中的'\n'就是换行符, 它在字符串中是不显示的, 但是匹配结果中又显示出来了</em>
复制代码
来源:https://www.cnblogs.com/blogLYP/p/17080272.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|
|
|
发表于 2023-2-2 03:12:10
举报
回复
分享
|
|
|
|