'Python中的正則表達式'
什麼是正則表達式
世界上分為兩種人,一種是懂正則表達式的,一種是不懂正則表達式的
按照一定的規則,從某個字符串中匹配出想要的數據,這個規則就是正則表達式
正則表達式常用的匹配規則
匹配某個字符串
text = 'hello'ret = re.match('he', text)print(ret.group())# >> he
Python資源共享群:626017123
點(.) 匹配任意的字符串
text = 'ab'ret = re.match('.', text)print(ret.group())# >> a
\\d 匹配任意的數字
text = '123'ret = re.match('\\d', text)print(ret.group())# >> 1
\\D 匹配任意的非數字
text = "a"ret = re.match('\\D',text)print(ret.group())# >> a
如果text為一個數字,那麼就匹配不成功了
text = "1"ret = re.match('\\D',text)print(ret.group())# >> AttributeError: 'NoneType' object has no attribute 'group'
\\s 匹配的是空白字符串(包括:\\n,\\t,\\r,空格)
text = "\\t"ret = re.match('\\s',text)print(ret.group())# >> 此處是一個空白
\\w 匹配的是 a-z 和 A-Z 以及數字和下劃線
text = "_"ret = re.match('\\w',text)print(ret.group())# >> _
如果要匹配一個其他的字符,那麼就匹配不到
text = "+"ret = re.match('\\w',text)print(ret.group())# >> AttributeError: 'NoneType' object has no attribute
\\W 匹配的是和 \\w 相反的
text = "+"ret = re.match('\\W',text)print(ret.group())# >> +
如果你的text是一個下劃線或者英文字符,那麼就匹配不到了
text = "_"ret = re.match('\\W',text)print(ret.group())# >> AttributeError: 'NoneType' object has no attribute
[] 組合的方式,只要滿足中括號中的某一項都算匹配成功
text = "027-88888888"ret = re.match('[\\d\\-]+',text)print(ret.group())# >> 027-88888888
其實可以使用中括號代替幾種默認的匹配規則
- \\d :[0-9]
- \\D :0-9
- \\w :[0-9a-zA-Z_]
- \\W :[^0-9a-zA-Z_]
匹配多個字符
*:可以匹配0或者任意多個字符
text = '8888'ret = re.match('\\d*',text)print(ret.group())# >> 8888
以上因為匹配的要求是 \\d ,那麼就要求是數字,後面跟了一個星號,就可以匹配到8888這四個字符
+:可以匹配1個或者多個字符,最少一個
text = "abc"ret = re.match('\\w+',text)print(ret.group())# >> abc
因為匹配的是\\w ,那麼就要求是英文字符,後面跟了一個加號,意味著最少要有一個滿足 \\w 的字符才能夠匹配到。如果text是一個空白字符或者是一個不滿足\\w的字符,就會報錯
text = ""ret = re.match('\\w+',text)print(ret.group())# >> AttributeError: 'NoneType' object has no attribute
?:匹配的字符可以出現一次或者不出現(0或者1)
text = "123"ret = re.match('\\d?',text)print(ret.group())# >> 1
{m}:匹配m個字符
text = "123"ret = re.match('\\d{2}',text)print(ret.group())# >> 12
{m,n}:匹配 m-n 個字符,在這中間的字符都可以匹配到
text = "123"ret = re.match('\\d{1,2}',text)prit(ret.group())# >> 12
如果text只有一個字符,也可以匹配出來
text = "1"ret = re.match('\\d{1,2}',text)prit(ret.group())# >> 1
幾個實際的案例(以給出的文本為例)
- 驗證手機號碼:手機號碼的規則是以1開頭,第二位可以是34587,後面那9位就可以隨意了
text = "18570631587"ret = re.match('1[34587]\\d{9}',text)print(ret.group())# >> 18570631587
- 如果是個不滿足條件的手機號碼。那麼就匹配不到了
text = "1857063158"ret = re.match('1[34587]\\d{9}',text)print(ret.group())# >> AttributeError: 'NoneType' object has no attribute
- 驗證郵箱:郵箱的規則是郵箱名稱是用數字、數字、下劃線組成的,然後是@符號,後面就是域名了
text = "[email protected]"ret = re.match('\\w+@\\w+\\.[a-zA-Z\\.]+',text)print(ret.group())
- 驗證URL:URL的規則是前面是http或者https或者是ftp然後再加上一個冒號,再加上一個斜槓,再後面就是可以出現任意非空白字符了
text = "http://www.baidu.com/"ret = re.match('(http|https|ftp)://[^\\s]+',text)print(ret.group())
- 驗證身份證:身份證的規則是,總共有18位,前面17位都是數字,後面一位可以是數字,也可以是小寫的x,也可以是大寫的X
text = "3113111890812323X"ret = re.match('\\d{17}[\\dxX]',text)print(ret.group())
^:表示以…開始
text = "hello"ret = re.match('^h',text)print(ret.group())
如果是在中括號中,代表的是取反操作
$:表示以…結束
# 匹配163.com的郵箱text = "[email protected]"ret = re.search('\\w+@163\\.com$',text)print(ret.group())# >> [email protected]
|:匹配多個表達式或者字符串
text = "hello|world"ret = re.search('hello',text)print(ret.group())# >> hello
貪婪模式和非貪婪模式
- 貪婪模式:正則表達式會匹配儘量多的字符,默認是貪婪模式。
- 非貪婪模式:正則表達式會盡量少的匹配字符。
text = "0123456"ret = re.match('\\d+',text)print(ret.group())# 因為默認採用貪婪模式,所以會輸出0123456
可以改成非貪婪模式,就只會匹配到0
text = "0123456"ret = re.match('\\d+?',text)print(ret.group())
匹配0-100之間的數字
text = '99'ret = re.match('[1-9]?\\d$|100$',text)print(ret.group())
如果text=101,就會拋出一個異常
text = '101'ret = re.match('[1-9]?\\d$|100$',text)print(ret.group())# >> AttributeError: 'NoneType' object has no attribute 'group'
轉義字符和原生字符
在正則表達式中,有些字符是有特殊意義的字符,在 Python 中 \\ 也是用來轉義的,因此如果想要在普通的字符串中匹配 \\ ,那麼就要給出 四個 \\
text = "apple \\c"ret = re.search('\\\\\\\\c',text)print(ret.group())
所以要使用原生字符就可以解決這個問題
text = "apple \\c"ret = re.search(r'\\\\c',text)print(ret.group())