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'
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
python 多進程和協(xié)程配合使用寫入數(shù)據(jù)
這篇文章主要介紹了python 多進程和協(xié)程配合使用寫入數(shù)據(jù),幫助大家利用python高效辦公,感興趣的朋友可以了解下2020-10-10python實現(xiàn)根據(jù)指定字符截取對應的行的內容方法
今天小編就為大家分享一篇python實現(xiàn)根據(jù)指定字符截取對應的行的內容方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python 的 __str__ 和 __repr__ 方法對比
這篇文章主要介紹了Python 的 __str__ 和 __repr__ 方法的相關資料,幫助大家區(qū)分__str__ 和 __repr__ ,感興趣的朋友可以了解下2020-09-09