python – 如何匹配多行和单行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何匹配多行和单行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3524字,纯文字阅读大概需要6分钟。
内容图文
我试图让我的头围绕一些正则表达式(使用Python 2.7)并且遇到了令人困惑的障碍.这与(.*)有关.我知道除非你使用标签re.DOTALL,否则dot会匹配除新行之外的所有内容.但是当我使用标签时,它包含太多.以下是我尝试过的一些变体和结果的代码:
import re
from urllib2 import urlopen
webpage = urlopen('http://trev.id.au/testfiles/rgxtxt.php').read()
# find the instances of pattern in the file
findPatHTMLComment = re.findall('<!--(.*)-->',webpage)
foundItems = len(findPatHTMLComment) # how many instances where found?
# Print results
print "Found " + str(foundItems) + " matches. They are: "
listIterator = []
listIterator[:]=range(0,foundItems)
for i in listIterator:
print "HTML_Comment["+ str(i) +"]: |" + findPatHTMLComment[i] + "| END HTML Comment"
这导致找到3个匹配,因为它没有找到多行注释部分.
使用:
findPatHTMLComment = re.findall('<!--(.*)-->',webpage,re.DOTALL)
使用文档末尾的第一个匹配查找单个匹配项.
findPatHTMLComment = re.findall('<!--(.*)-->',webpage,re.MULTILINE)
查找与第一个相同的内容,文件中的5个注释中只有3个.
问题:在这个例子中我应该使用什么作为正则表达式?你能为我和其他人解释一下吗?
感谢您提供的任何指导.感谢,并有一个愉快的一天.
编辑:包括上面代码中链接的样本数据(将很快从服务器中删除样本数据):
<html>
<!--[if lt IE 9 ]>
<script type="text/javascript">
jQuery(function ($) {
function TopSearchIE9(input,inputBtn){
var $topSearch=$(input);
var $topSearchBtn=$(inputBtn);
$topSearch.keydown(function(e) {
if (e.keyCode == 13) {
$topSearchBtn.trigger("click");
return false;
}
});
}
TopSearchIE9(".J-txt-focus1",".J-txt-focus1-btn");
TopSearchIE9(".J-txt-focus2",".J-txt-focus2-btn");
});
</script>
<![endif]-->
<!--[if lt IE 10 ]>
<style>
.new-header-search .hdSch-txt{ width: 225px;}
.new-header-search .hdSch-del{width: 0px; padding: 5px 0px;}
.new-header-search .hdSch-del.del{background:none; padding: }
</style>
<![endif]-->
<body>
<!-- This is a text file with a number of items to allow testing of some regex methods. It has no actual meaning -->
<div head1>Item heading for first item</div>
<!--By the way, this is a comment in a block of HTML text.-->
<div itembody>We can jump over the moon if we are fast enough, but we really shouldn't try it cause we may get a blood nose. When we do try and succeed it feels quite good.</div>
<div head1>Item heading for second item</div>
<div itembody>If this is showing, its the second body within the itembody div tags for this file</div>
<div head1>Item heading for the third item</div>
<div itembody>
Going to add another div tag
<div highlight>
and closing div tag
</div>
in this body to see how it handles that.
</div>
<!-- The above itembody data should
have it's own div and closing div tags -->
<div head1>Item heading for the fourth item</div>
<div itembody>
<p><a href="mailto:fred@flinstone.com">email fred</a> or phone him on +63 493 3382 3329 when you are ready to try more regex stuff.</p>
<p>You can also check with Barney by <a href="mailto:barney@rubble.com">emailing him</a> or phone him of +44 394 394 3992 if that is easier</p>
</div>
<!-- Thats all folks... -->
</body>
解决方法:
But when I do use the tag, it includes too much.
*是一个greedy运算符,意味着它将尽可能多地匹配,并且仍然允许正则表达式的其余部分匹配.你需要跟随*运算符吗?对于非贪婪的匹配,意味着“零或更多 – 最好尽可能少”.
re.findall('<!--(.*?)-->', webpage, re.DOTALL)
↑
re.MULTILINE标志称为多行,因为锚点^和$在实现时在多行上操作,在这种情况下使用多行修饰符是多余的.
另外,我会考虑使用BeautifulSoup来完成这项任务.
from bs4 import BeautifulSoup, Comment
soup = BeautifulSoup(html)
comments = soup.find_all(text=lambda text:isinstance(text, Comment))
内容总结
以上是互联网集市为您收集整理的python – 如何匹配多行和单行全部内容,希望文章能够帮你解决python – 如何匹配多行和单行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。