1. 程式人生 > >python中正則表示式庫re的使用(regex)

python中正則表示式庫re的使用(regex)

一、正則表示式庫的引用

在python中,你要使用正則表示式,需要引用程式庫re。

import re

二、正則表示式函式說明:

match =》嘗試在字串的開頭運用模式,返回一個match物件,如果沒有匹配則返回None fullmatch =》嘗試對整個字串運用模式,返回一個match物件,如果沒有匹配或部分匹配則返回None search =》 掃描整個字串來匹配模式,返回一個match物件,如果沒有找到匹配則返回None sub =》 找到模式匹配的字串並替換掉。返回替換了的字串 subn =》找到模式匹配的字串並替換掉。 返回替換了的字串和匹配的次數 split =》根據模式匹配來分割字串,返回子字串列表,列表中可能包含空的字串。
findall =》 查詢所有非重疊的匹配,返回的是一個組列表

三、簡單模式的使用

if m is not None: #food of the sea裡面開頭匹配foo: foo print("food of the sea裡面開頭匹配foo:",m.group())
print("") print("匹配多個字串符號|") pattern = "bet|bat|bit" str = "bat" m = re.match(pattern, str) if m is not None: #bat裡面開頭匹配bet|bat|bit = bat print("%s裡面開頭匹配%s = %s"
%(str, pattern, m.group()))
m = re.match(pattern, "blt") if m is None: #bat裡面匹配bet|bat|bit = None print("%s裡面開頭匹配%s = %s" % (str, pattern, "None"))
print("") print(".匹配任何單個的字元,除了\\n") pattern = ".end" str = "tend" m = re.match(pattern, "tend") if m is not None: #tend裡面開頭匹配.end = tend print("%s裡面開頭匹配%s = %s"
%(str, pattern, m.group()))
str = "end" m = re.match(pattern, str) if m is None: #end裡面開頭匹配.end = None print("%s裡面開頭匹配%s = %s" % (str, pattern, "None"))
str = "\nend" m = re.match(pattern, str) if m is None: #\nend裡面開頭匹配.end = None print("%s裡面開頭匹配%s = %s" % (str, pattern, "None")) print(".不能匹配\\n")
print("") print("建立字串類,[]") pattern = "[cr][23][dp][t2]" str = "c3pt" m = re.match(pattern, str) if m is not None: #c3pt裡面開頭匹配[cr][23][dp][t2] = c3pt print("%s裡面開頭匹配%s = %s" % (str, pattern, m.group()))
str = "c2d2" m = re.match(pattern, str) if m is not None: #c2d2裡面開頭匹配[cr][23][dp][t2] = c2d2 print("%s裡面開頭匹配%s = %s" % (str, pattern, m.group()))
str = "c2dd" m = re.match(pattern, str) if m is None: #c2dd裡面開頭匹配[cr][23][dp][t2] = None print("%s裡面開頭匹配%s = %s" % (str, pattern, "None"))
print("") print("匹配特殊字元") pattern = "\[email protected](\w+\.)?\w+\.com" str = "[email protected]" m = re.match(pattern, str) if m is not None: #[email protected]裡面開頭匹配\[email protected](\w+\.)?\w+\.com = [email protected] print("%s裡面開頭匹配%s = %s" % (str, pattern, m.group())) str = "[email protected]" m = re.match(pattern, str) if m is not None: #[email protected]裡面開頭匹配\[email protected](\w+\.)?\w+\.com = [email protected] print("%s裡面開頭匹配%s = %s" % (str, pattern, m.group())) str = "[email protected]" m = re.match(pattern, str) if m is None: #[email protected]裡面匹配\[email protected](\w+\.)?\w+\.com = None print("%s裡面開頭匹配%s = %s" % (str, pattern, "None"))

四、正則表示式函式的使用

print("") print("re模式匹配函式測試") print("測試fullmatch方法") pattern = ".end" str = "this is the end of film" m = re.fullmatch(pattern, str) if m is None: #this is the end of film裡面整體匹配.end = None print("%s裡面整體匹配%s = %s"%(str, pattern, "None")) str = "tend" m = re.fullmatch(pattern, str) if m is not None: #tend裡面整體匹配.end = tend print("%s裡面整體匹配%s = %s" % (str, pattern, m.group()))
print("") print("測試search方法") str = "this is the dend of film" m = re.search(pattern, str) if m is not None: #this is the dend of film裡面搜尋匹配.end = dend print("%s裡面搜尋匹配%s = %s" % (str, pattern, m.group()))
str = "this is the nd of film" m = re.search(pattern, str) if m is None: #this is the nd of film裡面搜尋匹配.end = None print("%s裡面搜尋匹配%s = %s" % (str, pattern, "None"))
print("") print("測試sub方法") pattern = "[aoe]" str = "mark the amoment" m = re.sub(pattern, "o", str) #mark the amoment被模式[aoe]替換後的字串mork tho omomont print("%s被模式%s替換後的字串%s"%(str, pattern, m)) str = "pip pip pug" m = re.sub(pattern, "o", str) #pip pip pug被模式[aoe]替換後的字串pip pip pug print("%s被模式%s替換後的字串%s"%(str, pattern, m))
print("") print("測試subn方法") pattern = "[aoe]" str = "mark the amoment" m = re.subn(pattern, "o", str) #mark the amoment被模式[aoe]替換後的字串('mork tho omomont', 5) print("%s被模式%s替換後的字串%s"%(str, pattern, m)) str = "pip pip pug" m = re.subn(pattern, "o", str) #pip pip pug被模式[aoe]替換後的字串('pip pip pug', 0) print("%s被模式%s替換後的字串%s"%(str, pattern, m))
print("") print("測試split方法") pattern = "of|the" str = "the book of the school, the nineth." m = re.split(pattern, str) #the book of the school, the nineth.被模式of|the分割後的列表['', ' book ', ' ', ' school, ', ' nineth.'] print("%s被模式%s分割後的列表%s"%(str, pattern, m))
print("") print("測試findall方法") pattern = "of|the" str = "the book of the school, the nineth." m = re.findall(pattern, str) #the book of the school, the nineth.被模式of|the分割後的列表['the', 'of', 'the', 'the'] print("%s被模式%s找到的字串列表%s"%(str, pattern, m))

相關推薦

python表示式re的使用regex

一、正則表示式庫的引用 在python中,你要使用正則表示式,需要引用程式庫re。 import re 二、正則表示式函式說明: match =》嘗試在字串的開頭運用模式,返回一個match物件

python 表示式用法 re.findall()

參考部落格原址:https://blog.csdn.net/YZXnuaa/article/details/79346963 <link rel="stylesheet" href="https://csdnimg.cn/relea

Python表示式re.match的用法

re.match(pattern, string, flags) 第一個引數是正則表示式,如果匹配成功,則返回一個Match,否則返回一個None; 第二個引數表示要匹配的字串; 第三個引數是標緻位,用於控制正則表示式的匹配方式,如:是否區分大小寫,多行匹配等等。 需要特別注意的是,這個方法並不是完

python學習-表示式re模塊

我只 com 返回 現在 輸出 -1 完全匹配 group clu python中的所有正則表達式函數都在re模塊中。import re導入該模塊。 1,創建正則表達式對象 想re.compile()傳入一個字符串值,表示正則表達式,它將返回一個Regex模式對象。 創建一

Python表示式常用函式sub,search,findall,split等使用

1.原生字串r python中字串前面加上 r 表示原生字串,不會轉義。與大多數程式語言相同,正則表示式裡使用"\"作為轉義字元,這就可能造成反斜槓困擾。假如你需要匹配文字中的字元"\",那麼使用程式語言表示的正則表示式裡將需要4個反斜槓"\\":前兩個和後兩個分別用於在程式語言裡轉義成反斜

Python表示式對單個字元,多個字元,匹配邊界等使用

         Regular Expression,正則表示式,又稱正規表示式、正規表示法、正則表示式、規則表示式、常規表示法(英語:Regular Expression,在程式碼中常簡寫為regex、regexp或RE),是電腦科學的一個概

python 歷險記(六python表示式的使用上篇

目錄 引言 什麼是正則表示式? 正則表示式有什麼用? 正則表示式的語法及使用例項 正則表示式語法有哪些? 這些正則到底該怎麼用? 小結 參考文件 系列文章列表 引言 剛接觸正則表示式,我也曾被它們天書似的符號組合給嚇住,但經過一段時間的深入

python表示式re模組

一.正則表示式中常用的字元含義 1、普通字元和11個元字元: 常用字元劃分 匹配範圍 示例資料 匹配的正則表示式 目標匹配的字串 普通字元 匹配自身 abc

python表示式詳解:特殊字元序列

內容提要: 說明:僅供學習交流使用 二、python正則表示式中的特殊字元序列 \number   \A  \Z   \b    \B    \d  \D   \s  \S  \w   \W      \\ 2.1\number  以相同的序號代表的組所匹配的內容

python表示式1

1.模式語言 1)任何字元只與其本身匹配 2)"."匹配任意字元 3)"*" 表示其前面那個字元可匹配0個或任意多個相同字元 4)"^"只匹配目標串的開頭 5)"$"只匹配目標串的結尾 2.原始字串 定義:在常規字串前加上r或者R字首. r'd:\test\1.t

python表示式的使用

正則表示式 python中需要使用正則表示式對字串進行匹配的時候,需要匯入re模組 #coding=utf-8 # 匯入re模組 import re # 使用match方法進行匹配操作 result = re.match(正

Python爬蟲之表示式的使用

import re html = ''' <div class="slide-page" style="width: 700px;" data-index="1"> <a class="item" target="_blank" href="https:

Python表示式介紹

正則 正則表通常是用來檢索、替換那些符合某個模式(規則)的文字。也就是說使用正則表示式可以在字串中匹配出你需要的字元或者字串,甚至可以替換你不需要的字元或者字串。 正則(不是python特有的) 匹配字串,其他語言也有正則表示式 例項: 需求:輸入字元,判斷字

Python3表示式使用方法崔慶才

  正則表示式 本節我們看一下正則表示式的相關用法,正則表示式是處理字串的強大的工具,它有自己特定的語法結構,有了它,實現字串的檢索、替換、匹配驗證都不在話下。   當然對於爬蟲來說,有了它,我們從HTML裡面提取我們想要的資訊就非常方便了。

表示式學習程序re.match()

#正則表示式學習筆記 import re """ .匹配任意字元 除換行符 {n}精確匹配前面n個表示式 \s 匹配任意的空白字元 \w 匹配字母數字及下劃線 .* 匹配任意除換行符之外的字元 {n} 精確匹配n個前面表示式。 + 匹配1個或多個的表示式。 ( )

Python-表示式-說明

正則表示式: (regular expression) —>>>正則表示式是用來簡潔表達一組字串的表示式 是一個描述字串模式的物件 正則表示式主要用來驗證使用者的資料, 以及對文字內容的資訊過濾,獲取滿足條件的內容 這樣做的好處是提高匹配效率,

表示式(python3-re模組示例

1.常用的正則表示式 '.' 預設匹配除\n之外的任意一個字元,若指定flag DOTALL,則匹配任意字元,包括換行 '^' 匹配字元開頭,若指定flags MULTILINE,這種也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE) '$'

表示式學習筆記

開始和結束  ^   $ '^' 表示開始  例如:'^the'  表示以 the 開頭的詞 '$' 表示結束  例如:'the$' 表示以 the 結尾的詞 'the' 表示 包含 the 的詞

表示式並不難

對於正則表示式,相信很多人都知道,但是很多人的第一感覺就是難學,因為看第一眼時,覺得完全沒有規律可尋,而且全是一堆各種各樣的特殊符號,完全不知所云。 其實只是對正則不瞭解而以,瞭解了你就會發現,原來就這樣啊正則所用的相關字元其實不多,也不難記,更不難懂,唯一難的就是組合起來之後,可讀性比較差,而

爬蟲入門系列表示式完全指南

爬蟲入門系列目錄: 正則表示式處理文字有如疾風掃秋葉,絕大部分程式語言都內建支援正則表示式,它應用在諸如表單驗證、文字提取、替換等場景。爬蟲系統更是離不開正則表示式,用好正則表示式往往能收到事半功倍的效果。 介紹正則表示式前,先來看一個問題,下面這段文字來自豆瓣的某個網頁連結,我對內容