正则表达式并不是Python的一部分。正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大。下面这篇文章主要介绍了关于Python正则表达式基础的相关资料,需要的朋友可以参考下。
前言
之前有人提了一个需求,我一看此需求用正则表达式最合适不过。考虑到之前每次使用正则表达式,都是临时抱佛脚,于是这次我就一边完成任务一边系统的学习了一遍正则表达式。主要参考PyCon2016上的一个视频Regular Expressions。
我将分几篇文章对正则表达式进行总结。
以下是第一部分,基础:
基础部分
这里总结了正则表达式最基础的用法,其中大部分内容对我(以及大部分程序员)来说都是平时经常用到的,所以我就一笔带过了,只对其中的几处用例子说明。
. 除了换行之外的其他所有字符
^ 行首
$ 行尾
[abcd] abcd其中的一个字符
[^abcd] 除了abcd之外的任意字符
[a-d] 相当于[abcd]
[a-dz] 相当于[abcdz]
\b 单词边界
\w 字母数字或下划线 相当于[a-zA-Z0-9_]
\W 与\w相反
\d 数字,相当于[0-9]
\D 与\d相反
\s 空白字符,相当于[ \t\n\r\f\v]
\S 与\s相反
{5} 在此之前的正则表达式部分(下同)准确的出现5次
{2,5} ~出现2到5次
{2,} ~出现2次或多次
{,5} ~出现0到5次
* ~出现0次或多次
? ~出现0次或1次
+ ~出现1次或多次
ABC|DEF 匹配ABC或者DEF
\ 转义字符,如\表示匹配*,\$表示匹配$*
\b、 \用以下几个例子简单说明一下:
\b:
>>> re.search(r'\bhello\b', 'hello') <_sre.SRE_Match object; span=(0, 5), match='hello'> >>> re.search(r'\bhello\b', 'hello world') <_sre.SRE_Match object; span=(0, 5), match='hello'> >>> re.search(r'\bhello\b', 'hello,world') <_sre.SRE_Match object; span=(0, 5), match='hello'> >>> re.search(r'\bhello\b', 'hello_world') >>>
其实这里,\b大体上和\W一支,但是\b可以匹配行首行尾等非显示类的字符,而\W不可以。
\:
>>> re.search(r'\$100', '$100') <_sre.SRE_Match object; span=(0, 4), match='$100'> >>> re.search(r'$100', '$100') >>>
想要匹配那些在正则表达式中有特殊含义的字符,如$、^、*等,就需要用\进行转义。
raw string:
另外,前面例子中,模式字符串(pattern)前面都加了一个r,这个r的意思是raw string,后面所接的字符串,Pyhton解释器无需对其进行转义。因为,\在Python字符串中和正则表达式中都有特殊含义,所以如果不是raw string,那么要表达一个\字符,就需要四个\了(在Python解释器中先转义一次,2个\表示1个\,剩下2个\,在正则表达式中又转义一次,最终剩下一个\)。例如:
>>> re.search(r'\bhello\b', 'hello') <_sre.SRE_Match object; span=(0, 5), match='hello'> >>> re.search('\bhello\b', 'hello') >>> re.search('\\bhello\\b', 'hello') <_sre.SRE_Match object; span=(0, 5), match='hello'> >>> re.search('\\\\hello\\\\', '\\hello\\') <_sre.SRE_Match object; span=(0, 7), match='\\hello\\'> >>> re.search(r'\\hello\\', '\\hello\\') <_sre.SRE_Match object; span=(0, 7), match='\\hello\\'> >>> print('\\hello\\') \hello\
【相关推荐】
1. Python免费视频教程
3. Python學習手冊
#以上是Python基礎篇之正規表達式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版
好用的JavaScript開發工具