python使用正規表示式連接符的範例程式碼

黄舟
發布: 2017-10-10 10:50:39
原創
1936 人瀏覽過

在正規表示式中,符合數字或英文字母的書寫非常不方便。因此,正則表達式引入了連接符“-”來定義字符的範圍,下面這篇文章主要給大家介紹了關於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使用正規表示式連接符的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!