熱線電話:13121318867

登錄
首頁精彩閱讀python中使用正則表達式的連接符示例代碼
python中使用正則表達式的連接符示例代碼
2018-06-26
收藏

python中使用正則表達式的連接符示例代碼

我們在前面的例子里,我們學習使用集合里字符或非集合里的字符,這時都是要把每個字符寫出來的,但是有時需要把26個小寫字母都放到集合里,那么按集合的方法,得輸入26次,一個一個鍵入去,這樣比較花時間,也容易出錯,那么有沒有更好的方法呢?這個是有的,就是使用正則表達式的連接符的功能:-,比如表示26個小寫字符,就使用[a-z]就可以了。
本文詳細的給大家介紹了關于python使用正則表達式的連接符的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
例子如下:    
#python 3.6
#蔡軍生
#http://blog.csdn.net/caimouse/article/details/51749579
#
from re_test_patterns import test_patterns
 
test_patterns(
 'This is some text -- with punctuation.',
 [('[a-z]+', 'sequences of lowercase letters'),
  ('[A-Z]+', 'sequences of uppercase letters'),
  ('[a-zA-Z]+', 'sequences of letters of either case'),
  ('[A-Z][a-z]+', 'one uppercase followed by lowercase')],
)

結果輸出如下:    
'[a-z]+' (sequences of lowercase letters)
 
 
 'This is some text -- with punctuation.'
 .'his'
 .....'is'
 ........'some'
 .............'text'
 .....................'with'
 ..........................'punctuation'
 
 
'[A-Z]+' (sequences of uppercase letters)
 
 
 'This is some text -- with punctuation.'
 'T'
 
 
'[a-zA-Z]+' (sequences of letters of either case)
 
 
 'This is some text -- with punctuation.'
 'This'
 .....'is'
 ........'some'
 .............'text'
 .....................'with'
 ..........................'punctuation'
 
 
'[A-Z][a-z]+' (one uppercase followed by lowercase)
 
 
 'This is some text -- with punctuation.'
 'This'
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值.

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢