前言
我們?cè)谇懊娴睦永铮覀儗W(xué)習(xí)使用集合里字符或非集合里的字符,這時(shí)都是要把每個(gè)字符寫(xiě)出來(lái)的,但是有時(shí)需要把26個(gè)小寫(xiě)字母都放到集合里,那么按集合的方法,得輸入26次,一個(gè)一個(gè)鍵入去,這樣比較花時(shí)間,也容易出錯(cuò),那么有沒(méi)有更好的方法呢?這個(gè)是有的,就是使用正則表達(dá)式的連接符的功能:-,比如表示26個(gè)小寫(xiě)字符,就使用[a-z]就可以了。
本文詳細(xì)的給大家介紹了關(guān)于python使用正則表達(dá)式的連接符的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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' )], ) |
結(jié)果輸出如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
'[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' |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:http://blog.csdn.net/caimouse/article/details/78174162