【362】python 正則表達式

參考:正則表達式 - 廖雪峰

參考:Python3 正則表達式 - 菜鳥教程

參考:正則表達式 - 教程


re.match 嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none。

re.search 掃描整個字符串并返回第一個成功的匹配。

  • span():返回搜索的索引區間
  • group():返回匹配的結果

re.sub 用于替換字符串中的匹配項。

re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回None;而re.search匹配整個字符串,直到找到一個匹配。

Python 的re模塊提供了re.sub用于替換字符串中的匹配項。

compile 函數用于編譯正則表達式,生成一個正則表達式( Pattern )對象,供 match() 和 search() 這兩個函數使用。

findall 在字符串中找到正則表達式所匹配的所有子串,并返回一個列表,如果沒有找到匹配的,則返回空列表。

注意:?match 和 search 是匹配一次 findall 匹配所有。

finditer 和 findall 類似,在字符串中找到正則表達式所匹配的所有子串,并把它們作為一個迭代器返回。

split 方法按照能夠匹配的子串將字符串分割后返回列表?


?\d?可以匹配一個數字;

\d?matches any digit, while?\D?matches any nondigit:

?\w?可以匹配一個字母或數字或者下劃線;

\w?matches any character that can be part of a word (Python identifier), that is, a letter, the underscore or a digit, while?\W?matches any other character:

?\W?可以匹配非數字字母下劃線;

?\s?表示一個空白格(也包括Tab、回車等空白格);

\s?matches any space, while?\S?matches any nonspace character:

?.?表示任意字符;

?*?表示任意字符長度(包括0個)(>=0);(其前面的一個字符,或者通過小括號匹配多個字符)

# 匹配最左邊,即是0個字符
>>> re.search('\d*', 'a123456b')
<_sre.SRE_Match object; span=(0, 0), match=''># 匹配最長
>>> re.search('\d\d\d*', 'a123456b')
<_sre.SRE_Match object; span=(1, 7), match='123456'>>>> re.search('\d\d*', 'a123456b')
<_sre.SRE_Match object; span=(1, 7), match='123456'># 兩個的倍數匹配
>>> re.search('\d(\d\d)*', 'a123456b')
<_sre.SRE_Match object; span=(1, 6), match='12345'>

  

?+?表示至少一個字符(>=1);(其前面的一個字符,或者通過小括號匹配多個字符)

>>> re.search('.\d+', 'a123456b')
<_sre.SRE_Match object; span=(0, 7), match='a123456'>>>> re.search('(.\d)+', 'a123456b')
<_sre.SRE_Match object; span=(0, 6), match='a12345'>

  

???表示0個或1個字符;(其前面的一個字符,或者通過小括號匹配多個字符)

>>> re.search('\s(\d\d)?\s', 'a 12 b')
<_sre.SRE_Match object; span=(1, 5), match=' 12 '>>>> re.search('\s(\d\d)?\s', 'a  b')
<_sre.SRE_Match object; span=(1, 3), match='  '>>>> re.search('\s(\d\d)?\s', 'a 1 b')
# 無返回值,沒有匹配成功

  

[] 匹配,同時需要轉義的字符,在里面不需要,如 [.] 表示點

>>> re.search('[.]', 'abcabc.123456.defdef')
<re.Match object; span=(6, 7), match='.'>>>> # 一次匹配中括號里面的任意字符
>>> re.search('[cba]+', 'abcabc.123456.defdef')
<re.Match object; span=(0, 6), match='abcabc'>>>> re.search('.[\d]*', 'abcabc.123456.defdef')
<re.Match object; span=(0, 1), match='a'>>>> re.search('\.[\d]*', 'abcabc.123456.defdef')
<re.Match object; span=(6, 13), match='.123456'>>>> re.search('[.\d]+', 'abcabc.123456.defdef')
<re.Match object; span=(6, 14), match='.123456.'>

?

?{n}?表示n個字符;

?{n,m}?表示n-m個字符;

?[0-9a-zA-Z\_]?可以匹配一個數字、字母或者下劃線;

?[0-9a-zA-Z\_]+?可以匹配至少由一個數字、字母或者下劃線組成的字符串,比如'a100''0_Z''Py3000'等等;

?[a-zA-Z\_][0-9a-zA-Z\_]*?可以匹配由字母或下劃線開頭,后接任意個由一個數字、字母或者下劃線組成的字符串,也就是Python合法的變量;

?[a-zA-Z\_][0-9a-zA-Z\_]{0, 19}?更精確地限制了變量的長度是1-20個字符(前面1個字符+后面最多19個字符)。

- 在 [] 中表示范圍,如果橫線挨著中括號則被視為真正的橫線
Ranges of letters or digits can be provided within square brackets, letting a hyphen separate the first and last characters in the range. A hyphen placed after the opening square bracket or before the closing square bracket is interpreted as a literal character:

>>> re.search('[e-h]+', 'ahgfea')
<re.Match object; span=(1, 5), match='hgfe'>>>> re.search('[B-D]+', 'ABCBDA')
<re.Match object; span=(1, 5), match='BCBD'>>>> re.search('[4-7]+', '154465571')
<re.Match object; span=(1, 8), match='5446557'>>>> re.search('[-e-gb]+', 'a--bg--fbe--z')
<re.Match object; span=(1, 12), match='--bg--fbe--'>>>> re.search('[73-5-]+', '14-34-576')
<re.Match object; span=(1, 8), match='4-34-57'>

^ 在 [] 中表示后面字符除外的其他字符

Within a square bracket, a caret after placed after the opening square bracket excludes the characters that follow within the brackets:

>>> re.search('[^4-60]+', '0172853')
<re.Match object; span=(1, 5), match='1728'>>>> re.search('[^-u-w]+', '-stv')
<re.Match object; span=(1, 3), match='st'>

?

?A|B?可以匹配A或B,所以(P|p)ython可以匹配'Python'或者'python'

Whereas square brackets surround alternative characters, a vertical bar separates alternative patterns:

>>> re.search('two|three|four', 'one three two')
<re.Match object; span=(4, 9), match='three'>>>> re.search('|two|three|four', 'one three two')
<re.Match object; span=(0, 0), match=''>>>> re.search('[1-3]+|[4-6]+', '01234567')
<re.Match object; span=(1, 4), match='123'>>>> re.search('([1-3]|[4-6])+', '01234567')
<re.Match object; span=(1, 7), match='123456'>>>> re.search('_\d+|[a-z]+_', '_abc_def_234_')
<re.Match object; span=(1, 5), match='abc_'>>>> re.search('_(\d+|[a-z]+)_', '_abc_def_234_')
<re.Match object; span=(0, 5), match='_abc_'>

?

?^?表示行的開頭,^\d表示必須以數字開頭。

?$?表示行的結束,\d$表示必須以數字結束。

A caret at the beginning of the pattern string matches the beginning of the data string; a dollar at the end of the pattern string matches the end of the data string:

>>> re.search('\d*', 'abc')
<re.Match object; span=(0, 0), match=''>>>> re.search('^\d*', 'abc')
<re.Match object; span=(0, 0), match=''>>>> re.search('\d*$', 'abc')
<re.Match object; span=(3, 3), match=''>>>> re.search('^\d*$', 'abc')>>> re.search('^\s*\d*\s*$', ' 345 ')
<re.Match object; span=(0, 5), match=' 345 '>

如果不在最前或最后,可以視為普通字符,但是在最前最后的時候想變成普通字符需要加上反斜杠

Escaping a dollar at the end of the pattern string, escaping a caret at the beginning of the pattern string or after the opening square bracket of a character class, makes dollar and caret lose the special meaning they have in those contexts context and let them be treated as literal characters:

>>> re.search('\$', '$*')
<re.Match object; span=(0, 1), match='$'>>>> re.search('\^', '*^')
<re.Match object; span=(1, 2), match='^'>>>> re.search('[\^]', '^*')
<re.Match object; span=(0, 1), match='^'>>>> re.search('[^^]', '^*')
<re.Match object; span=(1, 2), match='*'>

?

?^(\d{3})-(\d{3,8})$?分別定義了兩個組,可以直接從匹配的字符串中提取出區號和本地號碼:

  • group(0):永遠是原始字符串;
  • group(1):表示第1個子串;
  • group(2):表示第2個子串,以此類推。

分組順序:按照左括號的順序開始

Parentheses allow matched parts to be saved. The object returned by?re.search()?has a?group()?method that without argument, returns the whole match and with arguments, returns partial matches; it also has a?groups()method that returns all partial matches:

>>> R = re.search('((\d+) ((\d+) \d+)) (\d+ (\d+))','  1 23 456 78 9 0 ')>>> R
<re.Match object; span=(2, 15), match='1 23 456 78 9'>>>> R.group()
'1 23 456 78 9'>>> R.groups()
('1 23 456', '1', '23 456', '23', '78 9', '9')>>> [R.group(i) for i in range(len(R.groups()) + 1)]
['1 23 456 78 9', '1 23 456', '1', '23 456', '23', '78 9', '9']

?: 二選一,括號不計入分組

>>> R = re.search('([+-]?(?:0|[1-9]\d*)).*([+-]?(?:0|[1-9]\d*))',' a = -3014, b = 0 ')>>> R
<re.Match object; span=(5, 17), match='-3014, b = 0'>>>> R.groups()
('-3014', '0')

?

?

?.*?表示任意匹配除換行符(\n、\r)之外的任何單個或多個字符

模式描述
^匹配字符串的開頭
$匹配字符串的末尾。
.匹配任意字符,除了換行符,當re.DOTALL標記被指定時,則可以匹配包括換行符的任意字符。
[...]用來表示一組字符,單獨列出:[amk] 匹配 'a','m'或'k'
[^...]不在[]中的字符:[^abc] 匹配除了a,b,c之外的字符。
re*匹配0個或多個的表達式。
re+匹配1個或多個的表達式。
re?匹配0個或1個由前面的正則表達式定義的片段,非貪婪方式
re{ n}匹配n個前面表達式。例如,"o{2}"不能匹配"Bob"中的"o",但是能匹配"food"中的兩個o。
re{ n,}精確匹配n個前面表達式。例如,"o{2,}"不能匹配"Bob"中的"o",但能匹配"foooood"中的所有o。"o{1,}"等價于"o+"。"o{0,}"則等價于"o*"。
re{ n, m}匹配 n 到 m 次由前面的正則表達式定義的片段,貪婪方式
a| b匹配a或b
(re)匹配括號內的表達式,也表示一個組
(?imx)正則表達式包含三種可選標志:i, m, 或 x 。只影響括號中的區域。
(?-imx)正則表達式關閉 i, m, 或 x 可選標志。只影響括號中的區域。
(?: re)類似 (...), 但是不表示一個組
(?imx: re)在括號中使用i, m, 或 x 可選標志
(?-imx: re)在括號中不使用i, m, 或 x 可選標志
(?#...)注釋.
(?= re)前向肯定界定符。如果所含正則表達式,以 ... 表示,在當前位置成功匹配時成功,否則失敗。但一旦所含表達式已經嘗試,匹配引擎根本沒有提高;模式的剩余部分還要嘗試界定符的右邊。
(?! re)前向否定界定符。與肯定界定符相反;當所含表達式不能在字符串當前位置匹配時成功。
(?> re)匹配的獨立模式,省去回溯。
\w匹配數字字母下劃線
\W匹配非數字字母下劃線
\s匹配任意空白字符,等價于 [\t\n\r\f]。
\S匹配任意非空字符
\d匹配任意數字,等價于 [0-9]。
\D匹配任意非數字
\A匹配字符串開始
\Z匹配字符串結束,如果是存在換行,只匹配到換行前的結束字符串。
\z匹配字符串結束
\G匹配最后匹配完成的位置。
\b匹配一個單詞邊界,也就是指單詞和空格間的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。
\B匹配非單詞邊界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。
\n, \t, 等。匹配一個換行符。匹配一個制表符, 等
\1...\9匹配第n個分組的內容。
\10匹配第n個分組的內容,如果它經匹配。否則指的是八進制字符碼的表達式。

舉例:

?\d{3}?:匹配3個數字

?\s+?:至少有一個空格

?\d{3,8}?:3-8個數字

>>> mySent = 'This book is the best book on Python or M.L. I have ever laid eyes upon.'>>> mySent.split(' ')
['This', 'book', 'is', 'the', 'best', 'book', 'on', 'Python', 'or', 'M.L.', 'I', 'have', 'ever', 'laid', 'eyes', 'upon.']>>> import re>>> listOfTokens = re.split(r'\W*', mySent)>>> listOfTokens
['This', 'book', 'is', 'the', 'best', 'book', 'on', 'Python', 'or', 'M', 'L', 'I', 'have', 'ever', 'laid', 'eyes', 'upon', '']>>> [tok for tok in listOfTokens if len(tok) > 0]
['This', 'book', 'is', 'the', 'best', 'book', 'on', 'Python', 'or', 'M', 'L', 'I', 'have', 'ever', 'laid', 'eyes', 'upon']>>> [tok.lower() for tok in listOfTokens if len(tok) > 0]
['this', 'book', 'is', 'the', 'best', 'book', 'on', 'python', 'or', 'm', 'l', 'i', 'have', 'ever', 'laid', 'eyes', 'upon']>>> [tok.lower() for tok in listOfTokens if len(tok) > 2]
['this', 'book', 'the', 'best', 'book', 'python', 'have', 'ever', 'laid', 'eyes', 'upon']
>>> 

參考:python爬蟲(5)--正則表達式 - 小學森也要學編程 - 博客園??

實現刪除引號內部的內容,注意任意匹配使用【.*】

a = 'Sir Nina said: \"I am a Knight,\" but I am not sure'
b = "Sir Nina said: \"I am a Knight,\" but I am not sure"
print(re.sub(r'"(.*)"', '', a),
re.sub(r'"(.*)"', '', b), sep='\n')Output:
Sir Nina said:  but I am not sure
Sir Nina said:  but I am not sure

Example from Eric Martin's learning materials of COMP9021

The following function checks that its argument is a string:

  1. that from the beginning:?^
  2. consists of possibly some spaces:??*
  3. followed by an opening parenthesis:?\(
  4. possibly followed by spaces:??*
  5. possibly followed by either + or -:?[+-]?
  6. followed by either 0, or a nonzero digit followed by any sequence of digits:?0|[1-9]\d*
  7. possibly followed by spaces:??*
  8. followed by a comma:?,
  9. followed by characters matching the pattern described by 1-7
  10. followed by a closing parenthesis:?\)
  11. possibly followed by some spaces:??*
  12. all the way to the end:?$

Pairs of parentheses surround both numbers to match to capture them. For point 5, a surrounding pair of parentheses is needed;??:?makes it non-capturing:

>>> def validate_and_extract_payoffs(provided_input):pattern = '^ *\( *([+-]?(?:0|[1-9]\d*)) *,'\' *([+-]?(?:0|[1-9]\d*)) *\) *$'match = re.search(pattern, provided_input)if match:return (match.groups())>>> validate_and_extract_payoffs('(+0, -7 )')
('+0', '-7')>>> validate_and_extract_payoffs('  (-3014,0)  ')
('-3014', '0')

?

轉載于:https://www.cnblogs.com/alex-bn-lee/p/10325559.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388394.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388394.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388394.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在Python中使用Twitter Rest API批量搜索和下載推文

數據挖掘 &#xff0c; 編程 (Data Mining, Programming) Getting Twitter data獲取Twitter數據 Let’s use the Tweepy package in python instead of handling the Twitter API directly. The two things we will do with the package are, authorize ourselves to use the …

第一套數字電子計算機,計算機試題第一套

《計算機試題第一套》由會員分享&#xff0c;可在線閱讀&#xff0c;更多相關《計算機試題第一套(5頁珍藏版)》請在人人文庫網上搜索。1、計算機試題第一套1、計算機之所以能自動運算,就是由于采用了工作原理。A、布爾邏輯。B 儲存程序。C、數字電路。D,集成電路答案選B2、“長…

Windows7 + Nginx + Memcached + Tomcat 集群 session 共享

一&#xff0c;環境說明 操作系統是Windows7家庭版&#xff08;有點不專業哦&#xff0c;呵呵&#xff01;&#xff09;&#xff0c;JDK是1.6的版本&#xff0c; Tomcat是apache-tomcat-6.0.35-windows-x86&#xff0c;下載鏈接&#xff1a;http://tomcat.apache.org/ Nginx…

git 版本控制(一)

新建代碼庫repository 1、在當前目錄新建一個git代碼庫 git init git init projectname 2、下載一個項目&#xff0c;如果已經有了遠端的代碼&#xff0c;則可以使用clone下載 git clone url 增加/刪除/改名文件 1、添加指定文件到暫存區 git add filename 2、添加指定目錄到暫…

rollup學習小記

周末在家重構網關的Npm包&#xff0c;用到了rollup&#xff0c;記下筆記 rollup適合庫library的開發&#xff0c;而webpack適合應用程序的開發。 rollup也支持tree-shaking&#xff0c;自帶的功能。 package.json 也具有 module 字段&#xff0c;像 Rollup 和 webpack 2 這樣的…

大數據 vr csdn_VR中的數據可視化如何革命化科學

大數據 vr csdnAstronomy has become a big data discipline, and the ever growing databases in modern astronomy pose many new challenges for analysts. Scientists are more frequently turning to artificial intelligence and machine learning algorithms to analyze…

object-c 日志

printf和NSlog區別 NSLog會自動加上換行符&#xff0c;不需要自己添加換行符&#xff0c;NSLog會加上時間和進程信息&#xff0c;而printf僅將輸入的內容輸出不會添加任何額外的東西。兩者的輸入類型也是有區別的NSLog期待NSString*&#xff0c;而printf期待const char *。最本…

計算機真正管理的文件名是什么,計算機題,請大家多多幫忙,謝謝

4、在資源管理器中&#xff0c;若想顯示文件名、文件大小和文件類型&#xff0c;應采用什么顯示方式。( )A、小圖標顯示 B、列表顯示 C、詳細資料顯示 D、縮略圖顯示5、在EXCEL中&#xff0c;可以依據不同要求來提取和匯總數據&#xff0c;4、在資源管理器中&#xff0c;若想顯…

小a的排列

鏈接&#xff1a;https://ac.nowcoder.com/acm/contest/317/G來源&#xff1a;牛客網小a有一個長度為nn的排列。定義一段區間是"萌"的&#xff0c;當且僅當把區間中各個數排序后相鄰元素的差為11 現在他想知道包含數x,yx,y的長度最小的"萌"區間的左右端點 …

Xcode做簡易計算器

1.創建一個新項目&#xff0c;選擇“View-based Application”。輸入名字“Cal”&#xff0c;這時會有如下界面。 2.選擇Resources->CalViewController.xib并雙擊&#xff0c;便打開了資源編輯對話框。 3.我們會看到幾個窗口。其中有一個上面寫著Library&#xff0c;這里…

計算機 編程 教程 pdf,計算機專業教程-第3章編程接口介紹.pdf

下載第3章 編程接口介紹? DB2 UDB應用程序概述? 嵌入S Q L編程? CLI/ODBC應用程序? JAVA應用程序? DAO 、R D O 、A D O應用程序本章將介紹對DB2 UDB 可用的編程方法及其特色&#xff0c;其中一些方法附有簡單的例子&#xff0c;在這些例子中&#xff0c;有些并不是只適用…

導入數據庫怎么導入_導入必要的庫

導入數據庫怎么導入重點 (Top highlight)With the increasing popularity of machine learning, many traders are looking for ways in which they can “teach” a computer to trade for them. This process is called algorithmic trading (sometimes called algo-trading)…

windows查看系統版本號

windows查看系統版本號 winR,輸入cmd&#xff0c;確定&#xff0c;打開命令窗口&#xff0c;輸入msinfo32&#xff0c;注意要在英文狀態下輸入&#xff0c;回車。然后在彈出的窗口中就可以看到系統的具體版本號了。 winR,輸入cmd&#xff0c;確定&#xff0c;打開命令窗口&…

02:Kubernetes集群部署——平臺環境規劃

1、官方提供的三種部署方式&#xff1a; minikube&#xff1a; Minikube是一個工具&#xff0c;可以在本地快速運行一個單點的Kubernetes&#xff0c;僅用于嘗試Kubernetes或日常開發的用戶使用。部署地址&#xff1a;https://kubernetes.io/docs/setup/minikube/kubeadm Kubea…

更便捷的畫決策分支圖的工具_做出更好決策的3個要素

更便捷的畫決策分支圖的工具Have you ever wondered:您是否曾經想過&#xff1a; How did Google dominate 92.1% of the search engine market share? Google如何占領搜索引擎92.1&#xff05;的市場份額&#xff1f; How did Facebook achieve 74.1% of social media marke…

供來賓訪問計算機打開安全嗎,計算機安全設置操作手冊(22頁)-原創力文檔

計算機安全設置操作手冊ISO27001項目實施電腦配置(以XP為例)賬戶設置user每臺電腦設置administrator和user帳戶&#xff1b;管理員賬戶密碼至少 8位, 賬戶至少6位user將Administrator和user賬戶以外的其他賬戶禁用用具體步驟如下&#xff1a;、右擊【我的電腦】選擇【管理】如圖…

Windows半透明窗口開發技巧

Windows半透明窗口開發技巧 www.visual-gear.com 原創技術文章 在windows平臺上從窗口繪圖有兩種方法&#xff1a; 第一種響應窗口的WM_PAINT消息&#xff0c;使用窗口DC進行繪制 第二種是將窗口樣式設置為層窗口&#xff0c;即 WS_EX_LAYERED設置為該樣式之后窗口將不會產生任…

標識為普通SQL語法

在SQL語句的最前面增加 /*dialect*/轉載于:https://www.cnblogs.com/zouhuaxin/p/10333209.html

的界面跳轉

在界面的跳轉有兩種方法&#xff0c;一種方法是先刪除原來的界面&#xff0c;然后在插入新的界面&#xff1a;如下代碼 if (self.rootViewController.view.superview nil) { [singleDollController.view removeFromSuperview]; [self.view insertSubview:rootViewControlle…

計算性能提升100倍,Uber推出機器學習可視化調試工具

為了讓模型迭代過程更加可操作&#xff0c;并能夠提供更多的信息&#xff0c;Uber 開發了一個用于機器學習性能診斷和模型調試的可視化工具——Manifold。機器學習在 Uber 平臺上得到了廣泛的應用&#xff0c;以支持智能決策制定和特征預測&#xff08;如 ETA 預測 及 欺詐檢測…