日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python正则表达式集锦

發(fā)布時間:2025/3/21 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python正则表达式集锦 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

if 你只有5min:

  讀Python的正則提取技巧

  return

else:

Python正則表達式指南

Python正則表達式的用法

Python正則表達式操作指南


match和search的區(qū)別

??????? Python提供了兩種不同的原始操作:match和search。match是從字符串的起點開始做匹配,而search(perl默認)是從字符串做任意匹配。

??????? 注意:當正則表達式是' ^ '開頭時,match與search是相同的。match只有當且僅當被匹配的字符串開頭就能匹配 或 從pos參數(shù)的位置開始就能匹配 時才會成功。如下:


貪婪模式?,+,*,{m[,n]}

>>> import re >>> m = re.search("(book)+","mebookbookme") >>> m.group() 'bookbook' >>> m.groups() ('book',) >>> re.findall("(book)+","mebookbookme") ['book'] >>> m = re.search("((?:book)+)","mebookbookme") >>> m.group() 'bookbook' >>> m.groups() ('bookbook',) >>> re.findall("((?:book)+)","mebookbookme") ['bookbook'] >>> m = re.search("(book+)","mebookbookme") >>> m.group() 'book' >>> m.groups() ('book',) >>> re.findall("(book+)","mebookbookme") ['book', 'book']

python非貪婪、多行匹配正則表達式例子


Python 正則提取并賦值的問題

Python對unicode文件的讀寫和使用正則表達式

python類庫31[正則表達式]


常用正則表達式

驗證數(shù)字的正則表達式集

匹配特定數(shù)字:
^[1-9]\d*//數(shù)?[1?9]\d?//匹配正整數(shù)?[1?9]\d??  //匹配負整數(shù)
^-?[1-9]\d*//數(shù)[1?9]\d?|0//匹配整數(shù)[1?9]\d?|0  //匹配非負整數(shù)(正整數(shù) + 0)
^-[1-9]\d*|0//數(shù)數(shù)+0[1?9]\d?\.\d?|0\.\d?[1?9]\d?//匹配非正整數(shù)(負整數(shù)+0)[1?9]\d?\.\d?|0\.\d?[1?9]\d?   //匹配正浮點數(shù)
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)//數(shù)??([1?9]\d?\.\d?|0\.\d?[1?9]\d?|0?\.0+|0)//匹配負浮點數(shù)??([1?9]\d?\.\d?|0\.\d?[1?9]\d?|0?\.0+|0)  //匹配浮點數(shù)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0//數(shù)數(shù)+0(?([1?9]\d?\.\d?|0\.\d?[1?9]\d?))|0?\.0+|0//匹配非負浮點數(shù)(正浮點數(shù)+0)(?([1?9]\d?\.\d?|0\.\d?[1?9]\d?))|0?\.0+|0  //匹配非正浮點數(shù)(負浮點數(shù) + 0)
評注:處理大量數(shù)據(jù)時有用,具體應(yīng)用時注意修正

匹配特定字符串:
^[A-Za-z]+//26[A?Z]+//匹配由26個英文字母組成的字符串[A?Z]+  //匹配由26個英文字母的大寫組成的字符串
^[a-z]+//26[A?Za?z0?9]+//匹配由26個英文字母的小寫組成的字符串[A?Za?z0?9]+  //匹配由數(shù)字和26個英文字母組成的字符串
^\w+$  //匹配由數(shù)字、26個英文字母或者下劃線組成的字符串

匹配中文字符的正則表達式: [\u4e00-\u9fa5]
評注:匹配中文還真是個頭疼的事,有了這個表達式就好辦了

匹配雙字節(jié)字符(包括漢字在內(nèi)):[^\x00-\xff]
評注:可以用來計算字符串的長度(一個雙字節(jié)字符長度計2,ASCII字符計1)

匹配空白行的正則表達式:\n\s*\r
評注:可以用來刪除空白行

匹配HTML標記的正則表達式:<(\S*?)[^>]*>.*?</\1>|<.*? />
評注:網(wǎng)上流傳的版本太糟糕,上面這個也僅僅能匹配部分,對于復(fù)雜的嵌套標記依舊無能為力

匹配首尾空白字符的正則表達式:^\s*|\s*$
評注:可以用來刪除行首行尾的空白字符(包括空格、制表符、換頁符等等),非常有用的表達式

匹配Email地址的正則表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
評注:表單驗證時很實用

匹配網(wǎng)址URL的正則表達式:[a-zA-z]+://[^\s]*
評注:網(wǎng)上流傳的版本功能很有限,上面這個基本可以滿足需求

匹配帳號是否合法(字母開頭,允許5-16字節(jié),允許字母數(shù)字下劃線):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
評注:表單驗證時很實用

匹配國內(nèi)電話號碼:\d{3}-\d{8}|\d{4}-\d{7}
評注:匹配形式如 0511-4405222 或 021-87888822

匹配騰訊QQ號:[1-9][0-9]{4,}
評注:騰訊QQ號從10000開始

匹配中國郵政編碼:[1-9]\d{5}(?!\d)
評注:中國郵政編碼為6位數(shù)字

匹配身份證:\d{15}|\d{18}
評注:中國的身份證為15位或18位

匹配ip地址:\d+\.\d+\.\d+\.\d+
評注:提取ip地址時有用

?

整數(shù):^-?\d+數(shù)數(shù)+0\d+(\.\d+)?非負浮點數(shù)(正浮點數(shù)+0):\d+(\.\d+)?
正浮點數(shù)?? ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))數(shù)數(shù)+0((?\d+(\.\d+)?)|(0+(\.0+)?))非正浮點數(shù)(負浮點數(shù)+0)((?\d+(\.\d+)?)|(0+(\.0+)?))
負浮點數(shù)? ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))數(shù)(??\d+)(\.\d+)?浮點數(shù)(??\d+)(\.\d+)?

?

驗證非零的正整數(shù):^\+?[1-9][0-9]*數(shù)\-[1?9][0?9]?驗證非零的負整數(shù):\-[1?9][0?9]?
驗證非負整數(shù)(正整數(shù) + 0)? ^\d+數(shù)數(shù)+0((?\d+)|(0+))驗證非正整數(shù)(負整數(shù)+0)((?\d+)|(0+))

驗證數(shù)字:^[0-9]*n數(shù)\dn驗證n位的數(shù)字:\dn
驗證至少n位數(shù)字:^\d{n,}m?n數(shù)\dm,n驗證m?n位的數(shù)字:\dm,n
驗證零和非零開頭的數(shù)字:^(0|[1-9][0-9]*)數(shù)數(shù)[0?9]+(.[0?9]2)?驗證有兩位小數(shù)的正實數(shù):[0?9]+(.[0?9]2)?
驗證有1-3位小數(shù)的正實數(shù):^[0-9]+(.[0-9]{1,3})?$


驗證用戶密碼:^[a-zA-Z]\w{5,17}正確格式為:以字母開頭,長度在6-18之間,只能包含字符、數(shù)字和下劃線。?驗證是否含有?^%&',;=?正確格式為:以字母開頭,長度在6-18之間,只能包含字符、數(shù)字和下劃線。?驗證是否含有?^%&',;=?\" 等字符:[^%&',;=?\x22]+[\u4e00?\u9fa5],0,\x22]+驗證漢字:[\u4e00?\u9fa5],0,
驗證Email地址:^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*InternetURLhttp://([\w?]+\.)+[\w?]+(/[\w?./?驗證InternetURL:http://([\w?]+\.)+[\w?]+(/[\w?./??;^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?(\(\d3,4\)|\d3,4?)?\d7,8驗證電話號碼:(\(\d3,4\)|\d3,4?)?\d7,8:--正確格式為:XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX。
驗證身份證號(15位或18位數(shù)字):^\d{15}|\d{}1812(0?[1?9]|1[0?2])驗證一年的12個月:(0?[1?9]|1[0?2])?正確格式為:“01”-“09”和“1”“12”
驗證一個月的31天:^((0?[1-9])|((1|2)[0-9])|30|31)$??? 正確格式為:01、09和1、31。


DESCRIPTION
??? This module provides regular expression matching operations similar to
??? those found in Perl.? It supports both 8-bit and Unicode strings; both
??? the pattern and the strings being processed can contain null bytes and
??? characters outside the US ASCII range.
????
??? Regular expressions can contain both special and ordinary characters.
??? Most ordinary characters, like "A", "a", or "0", are the simplest
??? regular expressions; they simply match themselves.? You can
??? concatenate ordinary characters, so last matches the string 'last'.
????
??? The special characters are:
??????? "."????? Matches any character except a newline.
??????? "^"????? Matches the start of the string.
??????? ""??????Matches?the?end?of?the?string?or?just?before?the?newline?at??????????????????the?end?of?the?string.?????????"*"??????Matches?0?or?more?(greedy)?repetitions?of?the?preceding?RE.??????????????????Greedy?means?that?it?will?match?as?many?repetitions?as?possible.?????????"+"??????Matches?1?or?more?(greedy)?repetitions?of?the?preceding?RE.?????????"?"??????Matches?0?or?1?(greedy)?of?the?preceding?RE.?????????*?,+?,???Non-greedy?versions?of?the?previous?three?special?characters.?????????{m,n}????Matches?from?m?to?n?repetitions?of?the?preceding?RE.?????????{m,n}????Non-greedy?version?of?the?above.?????????"\\"?????Either?escapes?special?characters?or?signals?a?special?sequence.?????????[]???????Indicates?a?set?of?characters.??????????????????A?"^"?as?the?first?character?indicates?a?complementing?set.?????????"|"??????A|B,?creates?an?RE?that?will?match?either?A?or?B.?????????(...)????Matches?the?RE?inside?the?parentheses.??????????????????The?contents?can?be?retrieved?or?matched?later?in?the?string.?????????(?iLmsux)?Set?the?I,?L,?M,?S,?U,?or?X?flag?for?the?RE?(see?below).?????????(?:...)??Non-grouping?version?of?regular?parentheses.?????????(?P<name>...)?The?substring?matched?by?the?group?is?accessible?by?name.?????????(?P=name)?????Matches?the?text?matched?earlier?by?the?group?named?name.?????????(?#...)??A?comment;?ignored.?????????(?=...)??Matches?if?...?matches?next,?but?doesn't?consume?the?string.?????????(?!...)??Matches?if?...?doesn't?match?next.?????????(?<=...)?Matches?if?preceded?by?...?(must?be?fixed?length).?????????(?<!...)?Matches?if?not?preceded?by?...?(must?be?fixed?length).?????????(?(id/name)yes|no)?Matches?yes?pattern?if?the?group?with?id/name?matched,????????????????????????????the?(optional)?no?pattern?otherwise.??????????The?special?sequences?consist?of?"\\"?and?a?character?from?the?list?????below.??If?the?ordinary?character?is?not?on?the?list,?then?the?????resulting?RE?will?match?the?second?character.?????????\number??Matches?the?contents?of?the?group?of?the?same?number.?????????\A???????Matches?only?at?the?start?of?the?string.?????????\Z???????Matches?only?at?the?end?of?the?string.?????????\b???????Matches?the?empty?string,?but?only?at?the?start?or?end?of?a?word.?????????\B???????Matches?the?empty?string,?but?not?at?the?start?or?end?of?a?word.?????????\d???????Matches?any?decimal?digit;?equivalent?to?the?set?[0-9].?????????\D???????Matches?any?non-digit?character;?equivalent?to?the?set?[^0-9].?????????\s???????Matches?any?whitespace?character;?equivalent?to?[?\t\n\r\f\v].?????????\S???????Matches?any?non-whitespace?character;?equiv.?to?[^?\t\n\r\f\v].?????????\w???????Matches?any?alphanumeric?character;?equivalent?to?[a-zA-Z0-9_].??????????????????With?LOCALE,?it?will?match?the?set?[0-9_]?plus?characters?defined??????????????????as?letters?for?the?current?locale.?????????\W???????Matches?the?complement?of?\w.?????????\\???????Matches?a?literal?backslash.??????????This?module?exports?the?following?functions:?????????match????Match?a?regular?expression?pattern?to?the?beginning?of?a?string.?????????search???Search?a?string?for?the?presence?of?a?pattern.?????????sub??????Substitute?occurrences?of?a?pattern?found?in?a?string.?????????subn?????Same?as?sub,?but?also?return?the?number?of?substitutions?made.?????????split????Split?a?string?by?the?occurrences?of?a?pattern.?????????findall??Find?all?occurrences?of?a?pattern?in?a?string.?????????finditer?Return?an?iterator?yielding?a?match?object?for?each?match.?????????compile??Compile?a?pattern?into?a?RegexObject.?????????purge????Clear?the?regular?expression?cache.?????????escape???Backslash?all?non-alphanumerics?in?a?string.??????????Some?of?the?functions?in?this?module?takes?flags?as?optional?parameters:?????????I??IGNORECASE??Perform?case-insensitive?matching.?????????L??LOCALE??????Make?\w,?\W,?\b,?\B,?dependent?on?the?current?locale.?????????M??MULTILINE???"^"?matches?the?beginning?of?lines?(after?a?newline)????????????????????????as?well?as?the?string.????????????????????????""??????Matches?the?end?of?the?string?or?just?before?the?newline?at??????????????????the?end?of?the?string.?????????"*"??????Matches?0?or?more?(greedy)?repetitions?of?the?preceding?RE.??????????????????Greedy?means?that?it?will?match?as?many?repetitions?as?possible.?????????"+"??????Matches?1?or?more?(greedy)?repetitions?of?the?preceding?RE.?????????"?"??????Matches?0?or?1?(greedy)?of?the?preceding?RE.?????????*?,+?,???Non-greedy?versions?of?the?previous?three?special?characters.?????????{m,n}????Matches?from?m?to?n?repetitions?of?the?preceding?RE.?????????{m,n}????Non-greedy?version?of?the?above.?????????"\\"?????Either?escapes?special?characters?or?signals?a?special?sequence.?????????[]???????Indicates?a?set?of?characters.??????????????????A?"^"?as?the?first?character?indicates?a?complementing?set.?????????"|"??????A|B,?creates?an?RE?that?will?match?either?A?or?B.?????????(...)????Matches?the?RE?inside?the?parentheses.??????????????????The?contents?can?be?retrieved?or?matched?later?in?the?string.?????????(?iLmsux)?Set?the?I,?L,?M,?S,?U,?or?X?flag?for?the?RE?(see?below).?????????(?:...)??Non-grouping?version?of?regular?parentheses.?????????(?P<name>...)?The?substring?matched?by?the?group?is?accessible?by?name.?????????(?P=name)?????Matches?the?text?matched?earlier?by?the?group?named?name.?????????(?#...)??A?comment;?ignored.?????????(?=...)??Matches?if?...?matches?next,?but?doesn't?consume?the?string.?????????(?!...)??Matches?if?...?doesn't?match?next.?????????(?<=...)?Matches?if?preceded?by?...?(must?be?fixed?length).?????????(?<!...)?Matches?if?not?preceded?by?...?(must?be?fixed?length).?????????(?(id/name)yes|no)?Matches?yes?pattern?if?the?group?with?id/name?matched,????????????????????????????the?(optional)?no?pattern?otherwise.??????????The?special?sequences?consist?of?"\\"?and?a?character?from?the?list?????below.??If?the?ordinary?character?is?not?on?the?list,?then?the?????resulting?RE?will?match?the?second?character.?????????\number??Matches?the?contents?of?the?group?of?the?same?number.?????????\A???????Matches?only?at?the?start?of?the?string.?????????\Z???????Matches?only?at?the?end?of?the?string.?????????\b???????Matches?the?empty?string,?but?only?at?the?start?or?end?of?a?word.?????????\B???????Matches?the?empty?string,?but?not?at?the?start?or?end?of?a?word.?????????\d???????Matches?any?decimal?digit;?equivalent?to?the?set?[0-9].?????????\D???????Matches?any?non-digit?character;?equivalent?to?the?set?[^0-9].?????????\s???????Matches?any?whitespace?character;?equivalent?to?[?\t\n\r\f\v].?????????\S???????Matches?any?non-whitespace?character;?equiv.?to?[^?\t\n\r\f\v].?????????\w???????Matches?any?alphanumeric?character;?equivalent?to?[a-zA-Z0-9_].??????????????????With?LOCALE,?it?will?match?the?set?[0-9_]?plus?characters?defined??????????????????as?letters?for?the?current?locale.?????????\W???????Matches?the?complement?of?\w.?????????\\???????Matches?a?literal?backslash.??????????This?module?exports?the?following?functions:?????????match????Match?a?regular?expression?pattern?to?the?beginning?of?a?string.?????????search???Search?a?string?for?the?presence?of?a?pattern.?????????sub??????Substitute?occurrences?of?a?pattern?found?in?a?string.?????????subn?????Same?as?sub,?but?also?return?the?number?of?substitutions?made.?????????split????Split?a?string?by?the?occurrences?of?a?pattern.?????????findall??Find?all?occurrences?of?a?pattern?in?a?string.?????????finditer?Return?an?iterator?yielding?a?match?object?for?each?match.?????????compile??Compile?a?pattern?into?a?RegexObject.?????????purge????Clear?the?regular?expression?cache.?????????escape???Backslash?all?non-alphanumerics?in?a?string.??????????Some?of?the?functions?in?this?module?takes?flags?as?optional?parameters:?????????I??IGNORECASE??Perform?case-insensitive?matching.?????????L??LOCALE??????Make?\w,?\W,?\b,?\B,?dependent?on?the?current?locale.?????????M??MULTILINE???"^"?matches?the?beginning?of?lines?(after?a?newline)????????????????????????as?well?as?the?string.????????????????????????"" matches the end of lines (before a newline) as well
?????????????????????? as the end of the string.
??????? S? DOTALL????? "." matches any character at all, including the newline.
??????? X? VERBOSE???? Ignore whitespace and comments for nicer looking RE's.
??????? U? UNICODE???? Make \w, \W, \b, \B, dependent on the Unicode locale.
????
??? This module also defines an exception 'error'.

from:?http://www.cnblogs.com/wei-li/archive/2012/05/16/2505503.html

總結(jié)

以上是生活随笔為你收集整理的Python正则表达式集锦的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 97视频国产 | 日韩少妇高潮抽搐 | 亚洲一区无 | 国产精品无码一区二区三区在线看 | 韩日一区二区三区 | 久久久久久久九九九九 | 亚洲第一黄 | 超碰偷拍 | 亚洲国产精品天堂 | 前任攻略在线观看免费完整版 | av青青草原 | 95香蕉视频 | 九草在线观看 | 国产成人啪精品午夜在线观看 | 91视频久久久久 | 深田咏美在线x99av | 豆豆色成人网 | 欧美疯狂做受 | 亚洲AV无码乱码国产精品牛牛 | 国产女人与zoxxxx另类 | 男人操女人的免费视频 | 久久免费小视频 | 日本xxxwww| 婷婷91| 手机看片1024国产 | 精品在线视频免费观看 | 2020av在线| 国产乱码久久久久 | 亚洲大尺度av | 99视频观看 | 大地资源高清播放在线观看 | 中文字幕伦理 | 日韩激情在线观看 | 波多野结衣视频免费在线观看 | 人体私拍套图hdxxxx | 中文字幕成人一区 | 五月婷婷六月激情 | 大尺度激情吻胸视频 | 亚洲一区二区av在线 | 丁香伊人网 | 免费精品国产 | 午夜影院免费视频 | 欧美 日韩 国产 成人 在线 | 国产又爽又黄视频 | 国产精品成人网站 | 一二三区av | 玖草在线视频 | 国产在线欧美日韩 | 亚洲成av人影院 | 久久久久久欧美精品se一二三四 | 少妇2做爰bd在线意大利堕落 | 神马影院一区二区 | 黄色在线免费观看 | 欧美激情一级 | 中文字幕日韩三级 | 欧美亚洲综合在线 | 秋霞影院午夜丰满少妇在线视频 | 日本一区二区三区视频在线 | jizz免费在线观看 | av黄网站 | 国精产品乱码一区一区三区四区 | 欧美黄色大片视频 | 精品中文字幕一区二区三区 | 精品孕妇一区二区三区 | 四虎精品在线观看 | 久草小说 | 色网av| 日本一区不卡视频 | 日本久久久久久 | 91成人精品一区在线播放 | 亚洲精品乱码久久久久久麻豆不卡 | 一区二区三区在线播放视频 | 毛片毛片毛片毛片毛片毛片毛片 | 精品视频网 | 亚洲国产综合视频 | 黄色aa大片| 丰满大乳奶做爰ⅹxx视频 | 欧美日韩午夜激情 | 强乱中文字幕av一区乱码 | 日韩少妇内射免费播放 | 91爱爱视频| 国产高清一级 | 久草福利在线 | 丁香久久综合 | 久久久久久久免费 | 亚洲最大福利视频 | 尤物av无码色av无码 | 一级黄色片在线观看 | 国产精品成人av久久 | 免费av观看网址 | 91亚洲精品国偷拍自产在线观看 | 91免费网站在线观看 | 超碰97人人干 | 91涩| 亚洲精品乱码久久久久久 | 另类激情综合 | 国产精品色婷婷99久久精品 | 精品免费视频一区二区 | 国产亚洲精品电影 |