【发布时间】:2012-02-29 22:41:09
【问题描述】:
我想匹配 dotall 和 non-greedy。这就是我所拥有的:
img(.*?)(onmouseover)+?(.*?)a
然而,这并不是不贪婪。此数据与我预期的不匹配:
<img src="icon_siteItem.gif" alt="siteItem" title="A version of this resource is available on siteItem" border="0"></a><br><br></td><td rowspan="4" width="20"></td></tr><tr><td>An activity in which students find other more specific adjectives to
describe a range of nouns, followed by writing a postcard to describe a
nice holiday without using the word 'nice'.</td></tr><tr><td>From the resource collection: <a href="http://www.siteItem.co.uk/index.asp?CurrMenu=searchresults&tag=326" title="Resources to help work">Drafting </a></td></tr><tr><td><abbr style="border-bottom:0px" title="Key Stage 3">thing</abbr> | <abbr style="border-bottom:0px" title="Key Stage 4">hello</abbr> | <abbr style="border-bottom:0px" title="Resources">Skills</abbr></td></tr></tbody></table></div></div></td></tr><tr><td><div style="padding-left: 30px"><div><table style="" bgcolor="#DFE7EE" border="0" cellpadding="0" cellspacing="5" width="100%"><tbody><tr valign="top"><td rowspan="4" width="60"><a href="javascript:requiresLevel0(350,350);"><img name="/attachments/3700.pdf" onmouseover="ChangeImageOnRollover(this,'/application/files/images/attach_icons/rollover_pdf.gif')" onmouseout="ChangeImageOnRollover(this,'/application/files/images/attach_icons/small_pdf.gif')" src="small_pdf.gif" alt="Download Recognising and avoiding ambiguity in PDF format" title="Download in PDF format" style="vertical-align: middle;" border="0"></a><br>790.0 k<br>
我不明白为什么。
我认为我在上面的正则表达式中所说的是:
以“img”开头,然后允许 0 个或多个任何字符,包括换行符,然后查找至少 1 个“onmouseover”,然后允许 0 个或多个任何字符,包括换行符,然后是一个“a”
为什么这不符合我的预期?
关键点:必须启用dotall
【问题讨论】:
-
这似乎工作得很好。我在
img name="/attachments/3700.pdf" onmouseover="Cha上找到了匹配项 -
@jurgemaister 你启用了 dotall 吗?
-
不,我不知道。猜猜我没有仔细阅读这个问题。在这种情况下,它匹配从第二个字符到我上面提到的点的所有内容。这也是意料之中的。
-
非贪婪匹配意味着它将在第一个可能的字符处停止。这并不意味着它将从您似乎期望的最后一个可能的字符开始。
-
你实际上想用这个实现什么?如果我们知道目标和一些示例结果,就更容易提出改进建议......
标签: python regex non-greedy