日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python中正則表達(dá)式使用

系統(tǒng) 1863 0

python中正則表達(dá)式使用

文章目錄

  • python中正則表達(dá)式使用
      • 一、簡介
      • 二、使用
        • 2.1 常用規(guī)則
          • 2.1.1 正則表達(dá)式字符串寫法
          • 2.1.2 常用匹配規(guī)則
          • 2.1.3 貪婪與非貪婪匹配
        • 2.2 常用方法
          • 2.2.1 編譯
          • 2.2.2 匹配
          • 2.2.3 查找
          • 2.2.4 替換
          • 2.2.5 切分
        • 2.3 分組
          • 2.3.1 分組使用
          • 2.3.2 指定分組不捕獲
          • 2.3.3 分組特殊規(guī)則
        • 2.4 斷言

一、簡介

這里介紹python中的正則表達(dá)式使用,包含正則表達(dá)式常用規(guī)則、常用方法、貪婪與非貪婪匹配、分組、斷言等操作。

二、使用

這里預(yù)先定義待匹配字符串為:

            
              
                #待匹配字符串
              
              
                str
              
              
                =
              
              
                'make progress everyday ! 123456, and good aNd yes and haha AND 123 '
              
            
          

2.1 常用規(guī)則

2.1.1 正則表達(dá)式字符串寫法

正則表達(dá)式是一個(gè)字符串,在表達(dá)前添加 r 可以避免簡寫時(shí)對如 / 進(jìn)行轉(zhuǎn)譯。如:

            
              pat 
              
                =
              
               re
              
                .
              
              
                compile
              
              
                (
              
              r
              
                '\d+'
              
              
                )
              
            
          
2.1.2 常用匹配規(guī)則
            
              #符號
.:匹配除\n外的任意字符
[]:匹配中括號內(nèi)指定字符
[^]:匹配除中括號內(nèi)指定字符外的其他任意字符
():分組

#匹配簡寫
\d:匹配數(shù)字
\D:匹配非數(shù)字
\w:匹配數(shù)字、字母、下劃線
\W:匹配非數(shù)字、字母、下劃線
\s:匹配空白,空格和tab
\S:匹配非數(shù)字
\b:匹配數(shù)字
\B:匹配非數(shù)字

#匹配次數(shù)
*:匹配大于等于0次
+:匹配大于0次
?:匹配0或1次
{min,max}:匹配在min和max指定次數(shù)之間

#特殊規(guī)則
re.I 忽略大小寫,同re.IGNORECASE,或同分組中的(?i:正則表達(dá)式)。下面其他模塊類似
re.S 使.匹配包含換行符在內(nèi)的所有字符
re.M 多行匹配,影響開頭和結(jié)束符,即: ^和$
re.X 為了增加可讀性,忽略空格和  # 后面的注釋

            
          
2.1.3 貪婪與非貪婪匹配

匹配默認(rèn)是貪婪匹配(也就是盡量多的匹配字符串),正則后添加?即為非貪婪模式,如:.*? , \d+? ,
示例如:

            
              
                # 非貪婪
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              
                '\d+?'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # ['1', '2', '3', '4', '5', '6', '1', '2', '3']
              
              
                # 貪婪
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              
                '\d+'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # ['123456', '123']
              
            
          

2.2 常用方法

2.2.1 編譯
            
              
                #re.compile 預(yù)編譯正則,可多次使用,編譯后可直接調(diào)用相關(guān)函數(shù)
              
              
pat 
              
                =
              
               re
              
                .
              
              
                compile
              
              
                (
              
              r
              
                '\d+'
              
              
                )
              
              
                print
              
              
                (
              
              pat
              
                .
              
              findall
              
                (
              
              
                str
              
              
                )
              
              
                )
              
              
                #['123456', '123']
              
            
          
2.2.2 匹配
            
              
                # re.match 從文本開頭匹配,如果一開始就失敗,則返回None
              
              
obj 
              
                =
              
               re
              
                .
              
              match
              
                (
              
              
                'and'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                .
              
              group
              
                (
              
              
                )
              
              
                if
              
               obj 
              
                else
              
              
                'None'
              
              
                )
              
              
                #None
              
              
                # re.fullmatch 全文匹配
              
              
obj 
              
                =
              
               re
              
                .
              
              fullmatch
              
                (
              
              
                '.*?(\d+).*'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                .
              
              groups
              
                (
              
              
                )
              
              
                if
              
               obj 
              
                else
              
              
                'None'
              
              
                )
              
              
                #('123456',)
              
            
          
2.2.3 查找
            
              
                # re.search 從全文匹配,直到找到一處匹配返回
              
              
obj 
              
                =
              
               re
              
                .
              
              search
              
                (
              
              
                'and'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                .
              
              group
              
                (
              
              
                )
              
              
                if
              
               obj 
              
                else
              
              
                'None'
              
              
                )
              
              
                #and
              
              
                # re.findall 查找,返回所有
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              
                'and'
              
              
                ,
              
              
                str
              
              
                ,
              
               re
              
                .
              
              I
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #['and', 'aNd', 'and', 'AND']
              
              
                # re.finditer 查找,返回結(jié)果的迭代器
              
              
obj 
              
                =
              
               re
              
                .
              
              finditer
              
                (
              
              
                'and'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              
                list
              
              
                (
              
              
                map
              
              
                (
              
              
                lambda
              
               m
              
                :
              
               m
              
                .
              
              group
              
                (
              
              
                )
              
              
                ,
              
               obj
              
                )
              
              
                )
              
              
                )
              
              
                #['and', 'and']
              
            
          
2.2.4 替換
            
              
                # re.sub 字符串替換,直接字符串替換
              
              
obj 
              
                =
              
               re
              
                .
              
              sub
              
                (
              
              
                'and'
              
              
                ,
              
              
                '*and*'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # make progress everyday ! 123456, *and* good aNd yes *and* haha AND 1
              
              
                # re.sub 字符串替換,直接字符串替換,同時(shí)指定替換的次數(shù)
              
              
obj 
              
                =
              
               re
              
                .
              
              sub
              
                (
              
              
                'and'
              
              
                ,
              
              
                '*and*'
              
              
                ,
              
              
                str
              
              
                ,
              
              
                1
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # make progress everyday ! 123456, *and* good aNd yes and haha AND 123
              
              
                # # re.sub 字符串替換,直接字符串替換,忽略大小寫
              
              
obj 
              
                =
              
               re
              
                .
              
              sub
              
                (
              
              
                'and'
              
              
                ,
              
              
                '*and*'
              
              
                ,
              
              
                str
              
              
                ,
              
               flags
              
                =
              
              re
              
                .
              
              IGNORECASE
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # make progress everyday ! 123456, *and* good *and* yes *and* haha *and* 123
              
              
                # re.sub 字符串替換,函數(shù)替換, 替換函數(shù)也可使用lambda表達(dá)式,lambda m: '--{}--'.format(m.group())
              
              
                def
              
              
                repl
              
              
                (
              
              m
              
                )
              
              
                :
              
              
                return
              
              
                '--{}--'
              
              
                .
              
              
                format
              
              
                (
              
              m
              
                .
              
              group
              
                (
              
              
                )
              
              
                )
              
              
obj 
              
                =
              
               re
              
                .
              
              sub
              
                (
              
              
                'and'
              
              
                ,
              
               repl
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # make progress everyday ! 123456, --and-- good aNd yes --and-- haha AND 123
              
              
                # re.sub 字符串替換,函數(shù)替換, 替換函數(shù)使用lambda表達(dá)式,
              
              
obj 
              
                =
              
               re
              
                .
              
              sub
              
                (
              
              
                'and'
              
              
                ,
              
              
                lambda
              
               m
              
                :
              
              
                '--{}--'
              
              
                .
              
              
                format
              
              
                (
              
              m
              
                .
              
              group
              
                (
              
              
                )
              
              
                )
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # make progress everyday ! 123456, --and-- good aNd yes --and-- haha AND 123
              
              
                # re.subn 字符串替換,直接字符串替換,返回替換的字符串及替換的次數(shù)
              
              
obj 
              
                =
              
               re
              
                .
              
              subn
              
                (
              
              
                'and'
              
              
                ,
              
              
                '*and*'
              
              
                ,
              
              
                str
              
              
                ,
              
               flags
              
                =
              
              re
              
                .
              
              I
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # ('make progress everyday ! 123456, *and* good *and* yes *and* haha *and* 123 ', 4)
              
            
          
2.2.5 切分
            
              
                # re.split 分割
              
              
obj 
              
                =
              
               re
              
                .
              
              split
              
                (
              
              
                'and'
              
              
                ,
              
              
                str
              
              
                ,
              
               flags
              
                =
              
              re
              
                .
              
              I
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #['make progress everyday ! 123456, ', ' good ', ' yes ', ' haha ', ' 123 ']
              
            
          

2.3 分組

2.3.1 分組使用

使用()對指定表達(dá)式括起來即可分組。示例如下:

            
              obj 
              
                =
              
               re
              
                .
              
              match
              
                (
              
              
                '\D*(\d+)\D*(\d+)\D*'
              
              
                ,
              
              
                str
              
              
                )
              
              
                # 源字符串
              
              
                print
              
              
                (
              
              obj
              
                .
              
              group
              
                (
              
              
                )
              
              
                )
              
              
                #make progress everyday ! 123456, and good aNd yes and haha AND 123
              
              
                # 匹配的字符串元組
              
              
                print
              
              
                (
              
              obj
              
                .
              
              groups
              
                (
              
              
                )
              
              
                )
              
              
                (
              
              
                '123456'
              
              
                ,
              
              
                '123'
              
              
                )
              
              
                # 匹配字符串的最大位置索引
              
              
                print
              
              
                (
              
              obj
              
                .
              
              lastindex
              
                )
              
              
                #2
              
              
                print
              
              
                (
              
              obj
              
                .
              
              group
              
                (
              
              
                1
              
              
                )
              
              
                )
              
              
                #123456
              
              
                print
              
              
                (
              
              obj
              
                .
              
              group
              
                (
              
              
                2
              
              
                )
              
              
                )
              
              
                #123
              
              
                # 引用為實(shí)際分組字符串完全一樣,也就是引用的是文本,不是正則表達(dá)式
              
              
                # 分組直接引用,格式:\分組位置 , 如:\1 ,  \2
              
              
obj 
              
                =
              
               re
              
                .
              
              match
              
                (
              
              r
              
                '.*(\d).*\1.*'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                .
              
              groups
              
                (
              
              
                )
              
              
                )
              
              
                #('3',)
              
              
                # 分組命名,格式:(?P<分組名>正則表達(dá)式)
              
              
obj 
              
                =
              
               re
              
                .
              
              match
              
                (
              
              r
              
                '\D*(?P
                
                  \d+)\D*(?P
                  
                    \d+)\D*'
                  
                
              
              
                ,
              
              
                str
              
              
                )
              
              
                # 返回匹配串的key/val映射類型
              
              
                print
              
              
                (
              
              obj
              
                .
              
              groupdict
              
                (
              
              
                )
              
              
                )
              
              
                # {'numOne': '123456', 'numTwo': '123'}
              
              
                # 根據(jù)命名獲取匹配串映射類型
              
              
                print
              
              
                (
              
              obj
              
                .
              
              group
              
                (
              
              
                'numOne'
              
              
                )
              
              
                )
              
              
                # 123456
              
              
                # 分組命名引用,格式:(?P=引用的分組名)
              
              
obj 
              
                =
              
               re
              
                .
              
              match
              
                (
              
              r
              
                '.*(?P
                
                  and).*(?P=numOne).*'
                
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                .
              
              groups
              
                (
              
              
                )
              
              
                )
              
              
                #('and',)
              
              
                # 分組直接引用,格式:\分組位置 , 如:\1 ,  \2
              
              
obj 
              
                =
              
               re
              
                .
              
              match
              
                (
              
              r
              
                '.*(?P
                
                  and).*\1.*'
                
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                .
              
              groups
              
                (
              
              
                )
              
              
                )
              
              
                #('and',)
              
            
          
2.3.2 指定分組不捕獲

在分組()開頭添加 :? 表示該分組結(jié)果不捕獲,示例如下:

            
              
                # 內(nèi)部分組捕獲了
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              r
              
                '((and)\s+\w+)'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # [('and make', 'and'), ('and good', 'and'), ('and haha', 'and')]
              
              
                # 內(nèi)部分組沒有捕獲
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              r
              
                '((?:and)\s+\w+)'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                # ['and make', 'and good', 'and haha']
              
            
          
2.3.3 分組特殊規(guī)則

添加特殊規(guī)則,在分組()開頭使用?加i(同re.I,大小寫忽略,其他標(biāo)示類似), m, 或 x 標(biāo)示標(biāo)記,如(?i:正則表達(dá)式),
去掉特殊規(guī)則,在分組()開頭使用?-加i(同re.I,大小寫忽略,其他標(biāo)示類似), m, 或 x 標(biāo)示標(biāo)記,如(-i:正則表達(dá)式),
示例如:

            
              
                # 分組忽略大小寫
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              
                '(?i:and)'
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #['and', 'aNd', 'and', 'AND']
              
              
                # 正則表達(dá)式整體忽略大小寫,但指定分組不忽略大小寫
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              
                '(?-i:and)'
              
              
                ,
              
              
                str
              
              
                ,
              
               re
              
                .
              
              I
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #['and', 'and']
              
            
          

2.4 斷言

斷言可以對匹配的字符串前后進(jìn)行規(guī)定(對匹配的字符串前后再次添加條件限制)。

            
              
                # 后向肯定斷言,格式:(?=正則表達(dá)式)
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              r
              
                '(?P
                
                  \s*\w+\s*)(?=progress)'
                
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #['make ']
              
              
                # 前向肯定斷言,格式:(?<=正則表達(dá)式)
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              r
              
                '(?<=progress)(?P
                
                  \s*\w+\s*)'
                
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #[' everyday ']
              
              
                # 后向否定斷言,格式:(?!正則表達(dá)式)
              
              
obj 
              
                =
              
               re
              
                .
              
              findall
              
                (
              
              r
              
                '(?P
                
                  \w+\s+)(?!progress)'
                
              
              
                ,
              
              
                str
              
              
                )
              
              
                print
              
              
                (
              
              obj
              
                )
              
              
                #['progress ', 'everyday ', 'and ', 'good ', 'aNd ', 'yes ', 'and ', 'haha ', 'AND ', '123 ']
              
              
                # 前向否定斷言,格式:(?
              
            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 汉源县| 新源县| 洪洞县| 陇南市| 湖口县| 靖西县| 庄浪县| 错那县| 高要市| 德阳市| 新河县| 灵川县| 商河县| 肥西县| 昆明市| 双鸭山市| 肃北| 永修县| 遂溪县| 广平县| 射阳县| 保定市| 衡水市| 武功县| 青龙| 五莲县| 理塘县| 临潭县| 阿勒泰市| 大同县| 南安市| 福清市| 莱州市| 富裕县| 清水河县| 思茅市| 普格县| 通化市| 庆城县| 乐安县| 鄂温|