1. 程式人生 > >簡單正則表示式獲取手機號

簡單正則表示式獲取手機號

首先匯入re模組
# _*_coding:utf-8 _*_
import re

再得到原始資料

# 模擬手機號碼匹配
# 給定原始資料
myphone = "asdf15297901267asdf"

得到資料後根據個人需要來設定匹配表示式

# 提供匹配規則 \d表示匹配一個數字  {}表示限定字元表示式
myre = "\d{11}"

使用re模組中的findall方法,使用個人設定的表示式從原始資料中匹配資料

res = re.findall(myre,myphone)

最後列印結果為


# 若要將列表中的值取出則試用下標法
print res[0]