gpt4 book ai didi

python - 如何同时匹配多行和单行

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:51 32 4
gpt4 key购买 nike

我正试图了解一些正则表达式(使用 Python 2.7),但遇到了一个令人困惑的障碍。这与 (.*) 有关。我知道点匹配除了新行之外的所有内容,除非你使用标签 re.DOTALL。但是当我使用这个标签时,它包含的太多了。这是我尝试过的一些变体和结果的代码:

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 标志被称为多行,因为 anchor ^$ 在实现时在多行操作,这在这个使用多行修饰符的情况是多余的。

另一方面,我会考虑使用 BeautifulSoup为了这个任务。

from bs4 import BeautifulSoup, Comment
soup = BeautifulSoup(html)
comments = soup.find_all(text=lambda text:isinstance(text, Comment))

关于python - 如何同时匹配多行和单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215512/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com