python:正則模組

來源:互聯網
上載者:User

標籤:pos   pad   []   pre   span   com   技術分享   you   style   

1,Regex

Regex是用來做字串的匹配的,正則有他自己的規則,和python沒有關係,一種匹配字串的規則。

2,字元組

在同一個位置可能出現的各種字元組成了一個字元組,在Regex中用[]表示。字元分為很多類,比如數字、字母、標點等等。假如你現在要求一個位置"只能出現一個數字",那麼這個位置上的字元只能是0、1、2...9這10個數之一。
3,檢索輸入的手機號是否合法
#檢測輸入的手機號是否合法while True:    phone_number = input(‘please input your phone number : ‘)    if len(phone_number) == 11             and phone_number.isdigit()            and (phone_number.startswith(‘13‘)             or phone_number.startswith(‘14‘)             or phone_number.startswith(‘15‘)             or phone_number.startswith(‘18‘)):        print(‘是合法的手機號碼‘)    else:        print(‘不是合法的手機號碼‘)

4,換一種方法:調用re模組來檢測手機號是否合法

import rephone_number = input(‘please input your phone number : ‘)if re.match(‘^(13|14|15|18)[0-9]{9}$‘,phone_number):        print(‘是合法的手機號碼‘)else:        print(‘不是合法的手機號碼‘)

5,Regex規則

字元:

 

python:正則模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.