##1.正則模組匯入
python中內建正規表示式模組,使用正規表示式時應提前匯入該模組
import re
##2.正則命令
.
:匹配任意字元(\n除外)
^
:匹配字串的起始部分
$
:匹配字串的結束部分
*
:連續匹配0次或多次
+
:連續匹配1次或多次
?
:連續匹配0次或1次
|
:或。匹配|左右表示式任意乙個,從左到右匹配,如果|沒有包括在()中,則它的範圍是整個正規表示式
:連續匹配n次
:連續匹配m到n次
[0-9a-za-z]
:匹配乙個只能是0-9或a-z或a-z的數字或字母字元
[...]
:匹配來自字符集的任乙個字元
[...x-y...]
:匹配x-y範圍的任意乙個字元
[^0-9a-za-z]
:匹配任意乙個不能是0-9或a-z或a-z的字元
()
:代表乙個整體
\d
:匹配乙個十進位制數字字元
\d
:匹配乙個非數字字元
\w
:任何匹配乙個字母數字字元
\w
:匹配乙個非字母或數字的字元
\s
:匹配任意乙個空白符
\s
:匹配任意乙個非空白符
\b
:匹配任意乙個單詞邊界
\b
:[^\b]
\a
:匹配字串開頭,同^
\z
:匹配字串結尾,同$
\
.:匹配乙個字元"."
[\u4e00-\u9fa5]
:匹配中文
##3.re模組函式
(1)re.compile(pattern,flags=0)編譯正規表示式模式,返回乙個物件的模式。pattern: 編譯時用的表示式字串。flags 編譯標誌位,用於修改正規表示式的匹配方式,如:是否區分大小寫,多行匹配等。
t = "tina is a good girl, she is cool, clever, and so on..."
rr = re.compile(r'\w*oo\w*')
print(rr.findall(t)) #查詢所有包含'oo'的單詞
# ['good', 'cool']
(2)re.match(pattern, string, flags=0):決定re是否在字串剛開始的位置匹配。
注:這個方法並不是完全匹配。當pattern結束時若string還有剩餘字元,仍然視為成功。想要完全匹配,可以在表示式末尾加上邊界匹配符』$』
phone = input("輸入手機號:")
if re.match("^[1-9]\d$", phone) is not none:
print("合法")
else:
print("你輸錯了!")
(3)re.search(pattern, string, flags=0):在字串內查詢模式匹配,只要找到第乙個匹配然後返回,如果字串沒有匹配,則返回none。print(re.search('\dcom','www.4comrunoob.5com').group())
# 4com
(4)re.findall(pattern, string, flags=0):re.findall遍歷匹配,可以獲取字串中所有匹配的字串,返回乙個列表。p = re.compile(r'\d+')
print(p.findall('o1n2m3k4'))
# ['1', '2', '3', '4']
(5)re.finditer(pattern, string, flags=0): 搜尋string,返回乙個順序訪問每乙個匹配結果(match物件)的迭代器。找到 re 匹配的所有子串,並把它們作為乙個迭代器返回。item = re.finditer(r'\d+','12 drumm44ers drumming, 11 ... 10 ...')
for i in item:
print(i)
# <_sre.sre_match object; span=(0, 2), match='12'>
# <_sre.sre_match object; span=(8, 10), match='44'>
# <_sre.sre_match object; span=(24, 26), match='11'>
# <_sre.sre_match object; span=(31, 33), match='10'>
(6)re.split(pattern, string[, maxsplit]):按照能夠匹配的子串將string分割後返回列表。print(re.split('\d+','one1two2three3four4five5'))
# ['one', 'two', 'three', 'four', 'five', '']
(7)re.sub(pattern, repl, string, count):使用repl替換string中每乙個匹配的子串後返回替換後的字串。text = "jgood is a handsome boy, he is cool, clever, and so on..."
print(re.sub(r'\s+', '-', text))
# jgood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on...
(8)subn(pattern, repl, string, count=0, flags=0):返回替換次數text = "jgood is a handsome boy, he is cool, clever, and so on..."
print(re.subn(r'\s+', '-', text))
# ('jgood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on...', 11)
(9)re.fullmatch(pattern, string, flags=0):完全匹配name = input("輸入乙個數字:")
if re.fullmatch("^\d+\.?\d*$", name) is not none:
print(re.fullmatch("^\d+\.?\d*$", name).group())
else:
print("你輸錯了!")
Python基礎之正規表示式
本節將介紹python中正規表示式最基本的用法,正規表示式本身不做太多介紹。python中正規表示式的內建模組是re,最基本的用法是判斷某個字串是否符合某個表示式,分組,找出乙個字串中所有符合某個表示式的列表。可通過search 函式和match 函式來實現,不同之處是match函式是從字串的起始字...
Python 正規表示式(基礎)
正規表示式 regular expression 是乙個特殊的字串行,描述了一種字串匹配的模式可以用來檢查乙個串是否含有某種子串 將匹配的子串替換或者從某個串中取出符合某個條件的子串,或者是在指定的文章中,抓取特定的字串等。python處理正規表示式的模組是re模組,它是python語言擁有全部的正...
Python正規表示式基礎
直接給出字元就是精確匹配。特殊字元首先需要轉義如 d 匹配乙個數字,w 匹配乙個字母或者數字。123 d 可以匹配 1231 但是無法匹配 123a d d d 可以匹配到 123 w w w 可以匹配到 py3 表示任意乙個字元,py.可以表示py3 py 等 表示任意長個字元,表示至少乙個字元,...