Python-re模块-正则表达式

正则表达式

  正则表达式本质上是一种小型的、高度专业化的编程语言

    通过正则表达式可以判断某个字符串是否匹配过滤逻辑

    通过正则表达式可以从字符串中获取指定部分

    可以迅速的用极简单的方式达到字符串的复杂控制

   元字符 . ^ $ * + ? | {} [] ()  \

    .  匹配除了\r \n以外的所有字符,一个.匹配一个字符

# 'al.x' 可以匹配到 'alexads' 中的 'alex'

    ^ 必须在开头才能匹配  

# '^alex' 可以匹配到 'alexads' 中的'alex';匹配不到 'adsalex' 中的'alex'

    $  必须在结尾才能匹配           

# 'alex$' 可以匹配到 'adsalex$' 中的'alex';匹配不到'alexads' 中的'alex'

    *  匹配最近的字符,0到多次        

# 'alex*' 可以匹配到 'ale' 和 'alexxxxxxxx'

    +  匹配最近的字符,1到多次

# 'alex+' 可以匹配到 'alex' 和 'alexxxxxxxx'

    ? 匹配最近的字符,0或1次

# 'alex?' 可以匹配到 'ale' 和 'alex'

    |  或操作

# 'alex?' 可以匹配到 'ale' | 'alex'  

    {} 重复固定次数,或一个闭区间次数

# 'alex{3,5}' 可以匹配到 'alexxx'、'alexxxx'、'alexxxxx' ; 
# x的个数可以超过5,'alex{3,5}'都是其中的一部分
          

    [] 字符集

      大部分元字符在字符集中回归本意,只是普通的字符

      \ 在字符集中功能不变

      字符集中的字符是或的关系

# 'al[ex]' 在 字符串 'alexxxxalxex' 中可以匹配到两部分:'ale'  'alx' 

      - 在字符集中表示从...到...   

# [0-9] 表示数字0、1、2、3、4、5、6、7、8、9
# [A-Z] 表示26个大写字母

      ^ 在字符集中为 非操作,取反操作

# [^0-9] 表示除了数字0-9意外的字符

    () 组,作为一个整体

    \  后面加元字符,元字符变为普通字符;转义符

     后面加普通字符,实现特殊的功能

       引用序号对应的字组所匹配的字符串

# "(alex)(eric)com\2"
# 等同于
# "(alex)(eric)com(eric)"

      \d 匹配任意的十进制数    [0-9]

      \D 匹配任意非数字字符    [^0-9]

      \s 匹配任何空格字符      [ \t\n\r\f\v]                           

      \S 匹配任何非空格字符    [^ \t\n\r\f\v]

      \w 匹配任何字母数字字符   [a-zA-Z0-9]

      \W 匹配任何非字母数字字符  [^a-zA-Z0-9]

      \b 只是匹配字符串开头结尾及空格回车等的位置,不会匹配空格符本身

# "\babc" 可以匹配到 " abc sd "  中的 " abc "

    正则表达式默认为贪婪匹配,按最多匹配

    在匹配规则后加?表示非贪婪匹配,按最少次数匹配;如果前后有限定条件,则正常匹配

 

Python中使用正则表达式

  引入re模块

import re

  常用方法  

    注:pattern:正则表达式

      string:要判断的字符串

      flags:搜索模式(具体可参照源码的flags 常量定义)

          findall(pattern, string, flags=0)  

      #返回字符串中的所有非重叠匹配的列表;匹配不到返回空

import re

str1 = '12 drumm44ers drumming, 11 ... 10 ...'
tmp = re.findall(r'\d+', str1)
print(tmp)
#打印:
# ['12', '44', '11', '10']

      #匹配规则为多个,将每个规则匹配到的字符串作为元祖元素

      #将该元祖作为列表的元素,再将列表作为返回值

      #匹配不到返回空

str1 = '12 drumm44ers drumming, 11 ... 10 ...'
tmp = re.findall(r'(\d+)(\s)', str1)
print(tmp)
#打印:
[('12', ' '), ('11', ' '), ('10', ' ')]

    finditer(pattern, string, flags=0)  #将字符串中所有非重叠的匹配元素作为可迭代序列返回

ite = re.finditer("qwe","qwertydwqweasd")
for i in ite:
    print(i.group())
#打印
#qwe
#qwe

    compile(pattern, flags=0)  # 将pattern表达式编译为对象,使用这个对象进行字符串的匹配

import re

str1 = '12 drumm44ers drumming, 11 ... 10 ...'
p = re.compile(r'\d+')
tmp = p.findall(str1)
print(tmp)
#输出
#['12' , '44' , '11' , '10']

    escape(pattern)  #对字符串中的非字母数字进行转义

    match(pattern , string , flags=0)  #从字符串首位开始匹配;返回match对象;否则返回None

r = re.match("qwe" , "qwerty")
print(r)
#打印:
#<_sre.SRE_Match object; span=(0, 3), match='qwe'>

    fullmatch(pattern , string , flags=0)  #两个字符串完全匹配,返回match对象;否则返回None

r = re.fullmatch("qwerty" , "qwerty")
print(r)
#打印:
#<_sre.SRE_Match object; span=(0, 6), match='qwerty'>

    search(pattern , string , flags=0)  #查询pattern是否在string中匹配,匹配到返回match对象;否则返回None

r = re.search("qwe" , "123qweqwerty")
print(r)
#打印:
#<_sre.SRE_Match object; span=(3, 6), match='qwe'>

    purge()  #清除正则表达式缓存

    split(pattern , string , maxsplit=0 , flags=0)  

    #以pattern进行匹配分割,返回列表对象;匹配不到,将string作为列表元素返回

r = re.split("w" , "qwerty")
print(r)
#打印:
#['q', 'erty']

r = re.split("1" , "qwerty")
print(r)
#打印:
#['qwerty']

    sub(pattern , repl , string , count=0 ,     flags=0) 

    #  旧    新  源  重复时修改次数   搜索模式

    #使用新字符串替换字符串中的某部分,如果该部分重复出现,默认全部替换,count可控制替换次数

    #返回替换后的字符串

r = re.sub("qw" , "wq" , "qwqwerty")
print(r)
#打印:wqwqerty

r = re.sub("qw" , "wq" , "qwqwerty" , count=1)
print(r)
#打印:wqqwerty

    subn(pattern , repl , string , count=0 , flags=0)

    #作用同sub();

    #返回值为替换后的字符串,修改次数(元祖)

r = re.subn("qw" , "wq" , "qwertyqw")
print(r)
#打印:
#('wqertywq', 2)

    template()  #编译一个匹配规则,并返回为对象

r = re.template("qw")
print(r)
#打印:re.compile('qw', re.TEMPLATE)

w = re.subn(r , "wq" , "qwertyqwe")
print(w)
#打印:('wqertywqe', 2) 
 

 

posted @ 2016-05-16 16:54  阿金study  阅读(94)  评论(0)    收藏  举报