python的正則表達(dá)式

在進(jìn)行正則表達(dá)式的匹配時(shí)以下四個(gè)語(yǔ)句的區(qū)別到底在哪里?

需要匹配的原始文檔:

<div class="author clearfix">
<a href="/users/24041432/" target="_blank" rel="nofollow">
![做個(gè)被需要的人。](http://upload-images.jianshu.io/upload_images/4772802-a9e4f303f36042cf.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
</a>
<a href="/users/24041432/" target="_blank" title="做個(gè)被需要的人。">
<h2>做個(gè)被需要的人。</h2>
</a>
...
<div class="signin-error" id="signin-error"></div>
<button type="submit" id="form-submit" class="form-submit">登錄</button>
</form>
</div>
<div class="signin-foot clearfix">
<a rel="nofollow" href="/new4/fetchpass" class="fetch-password">忘記密碼?</a>
</div>
</div>
</div>

<div class="float-nav">
<a class="float-nav-backtop" href="#" rel="nofollow">
<span class="float-nav-backtop-icon"></span>
</a>
</div>

<!--[if gte IE 6]>
<script type="text/javascript" src="http://static.qiushibaike.com/js/src/web/json3.js?v=3a7f66a11a09842cd7555fad039657be"></script>
<![endif]-->
<script type="text/javascript" src="http://static.qiushibaike.com/js/dist/web/libs.min.js?v=bc8ddd36f0e7fed7c27f437c17f23ce0"></script>
<script type="text/javascript" src="http://static.qiushibaike.com/js/dist/web/app.min.js?v=9d24424ee8d18d35a0a64362f6e6694a"></script>



<script type="text/javascript">

期望的第二個(gè)匹配集合的輸出

做個(gè)被需要的人。

語(yǔ)句1:

'<div.*?author clearfix">(.*?)<a.*?title="(.*)">'
items = re.findall(pattern, content)
for item in items:
  print item[1]

輸出:

做個(gè)被需要的人。">
<h2>做個(gè)被需要的人。</h2>
</a>
<div class="articleGender manIcon">21</div>
</div>


<input type="hidden" id="hid">
<div id="single-next-link" title="下一條">


<div class="content">
...
<script type="text/javascript

語(yǔ)句2:

'<div.*?author clearfix">(.*?)<a.*?title="(.*)">.*?<h2>'
items = re.findall(pattern, content)
for item in items:
  print item[1]

輸出:

做個(gè)被需要的人。

語(yǔ)句3:

'<div.*?author clearfix">(.*?)<a.*?title="(.*?)">'
items = re.findall(pattern, content)
for item in items:
  print item[1]

輸出:

做個(gè)被需要的人。

語(yǔ)句4:

'<div.*?author clearfix">.*?<a.*?title="(.*?)">'
items = re.findall(pattern, content)
for item in items:
  print item[0]

輸出:

一些觀(guān)察和分析

  • 觀(guān)察
  • findall()的返回值是什么?
    我們?cè)诖a中打印出findall的返回值:
    pattern = re.compile('<div.*?author clearfix">(.*?)<a.*?title="(.*?)">', re.S)
    items = re.findall(pattern, content)
    print items

輸出:

[(u'\n', u'\u505a\u4e2a\u88ab\u9700\u8981\u7684\u4eba\u3002')]

由此輸出,我們可以看出items包含了兩個(gè)匹配集合。一個(gè)是換行符,一個(gè)是一段文本。

  • 再試一個(gè)
    輸入
pattern = re.compile('<div.*?author clearfix">.*?<a.*?title="(.*?)">', re.S)
    items = re.findall(pattern, content)
    print items

輸出

[u'\u505a\u4e2a\u88ab\u9700\u8981\u7684\u4eba\u3002']

由此輸出,可以發(fā)現(xiàn)items僅僅是我們用括號(hào)制定的匹配集合。對(duì)于上面的一個(gè)例子同樣成立。通過(guò)觀(guān)察,我們發(fā)現(xiàn),有一次記錄匹配,items中就會(huì)多一個(gè)item, 而對(duì)于每個(gè)匹配的item,它內(nèi)部的元素就是每個(gè)匹配集合。

  • 分析
  • 語(yǔ)句1中采用的是python默認(rèn)的貪婪匹配,所以這個(gè)pattern直接匹配到了文本的最后。
  • 語(yǔ)句2中采用的也是貪婪匹配,但是因?yàn)樵谖覀兤谕哪莻€(gè)pattern之后加上了對(duì)于<h2>的匹配,所以就限制了匹配的長(zhǎng)度。
  • 通過(guò)以上兩次實(shí)驗(yàn)的分析我們可以得知,對(duì)于語(yǔ)句3, items中有且僅有一個(gè)元素。而item[1]就是第二個(gè)匹配集,即title的值。
  • 對(duì)于輸出4而言,item中只有一個(gè)匹配集合。所以item就是“做個(gè)被需要的人”。因?yàn)橐粋€(gè)字符串就是字符的數(shù)組,所以在我們使用item[0]時(shí)就獲取了第一個(gè)字符。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容