码迷,mamicode.com
首页 > 其他好文 > 详细

re模块

时间:2017-02-13 22:03:01      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:key   pytho   pat   内容   多个   取出   code   which   log   

python中re模块提供了正则表达式相关操作

 字符:

  . 匹配除换行符以外的任意字符
  \w 匹配字母或数字或下划线或汉字
  \s 匹配任意的空白符
  \d 匹配数字
  \b 匹配单词的开始或结束
  ^ 匹配字符串的开始
  $ 匹配字符串的结束

次数:

  * 重复零次或更多次
  + 重复一次或更多次
  ? 重复零次或一次
  {n} 重复n次
  {n,} 重复n次或更多次
  {n,m} 重复n到m次

match

 1 # match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None
 2  
 3  
 4  match(pattern, string, flags=0)
 5  # pattern: 正则模型
 6  # string : 要匹配的字符串
 7  # falgs  : 匹配模式
 8      X  VERBOSE     Ignore whitespace and comments for nicer looking REs.
 9      I  IGNORECASE  Perform case-insensitive matching.
10      M  MULTILINE   "^" matches the beginning of lines (after a newline)
11                     as well as the string.
12                     "$" matches the end of lines (before a newline) as well
13                     as the end of the string.
14      S  DOTALL      "." matches any character at all, including the newline.
15  
16      A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D
17                     match the corresponding ASCII character categories
18                     (rather than the whole Unicode categories, which is the
19                     default).
20                     For bytes patterns, this flag is the only available
21                     behaviour and neednt be specified.
22       
23      L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.
24      U  UNICODE     For compatibility only. Ignored for string patterns (it
25                     is the default), and forbidden for bytes patterns.
技术分享
# 无分组
        r = re.match("h\w+", origin)
        print(r.group())     # 获取匹配到的所有结果
        print(r.groups())    # 获取模型中匹配到的分组结果
        print(r.groupdict()) # 获取模型中匹配到的分组结果

        # 有分组

        # 为何要有分组?提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来)

        r = re.match("h(\w+).*(?P<name>\d)$", origin)
        print(r.group())     # 获取匹配到的所有结果
        print(r.groups())    # 获取模型中匹配到的分组结果
        print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组
Demo

search

1 # search,浏览整个字符串去匹配第一个,未匹配成功返回None
2 # search(pattern, string, flags=0)
技术分享
# 无分组

        r = re.search("a\w+", origin)
        print(r.group())     # 获取匹配到的所有结果
        print(r.groups())    # 获取模型中匹配到的分组结果
        print(r.groupdict()) # 获取模型中匹配到的分组结果

        # 有分组

        r = re.search("a(\w+).*(?P<name>\d)$", origin)
        print(r.group())     # 获取匹配到的所有结果
        print(r.groups())    # 获取模型中匹配到的分组结果
        print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组
Demo

findall

1 # findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;
2 # 空的匹配也会包含在结果中
3 #findall(pattern, string, flags=0)
技术分享
# 无分组
        r = re.findall("a\w+",origin)
        print(r)

        # 有分组
        origin = "hello alex bcd abcd lge acd 19"
        r = re.findall("a((\w*)c)(d)", origin)
        print(r)
Demo

sub

1 # sub,替换匹配成功的指定位置字符串
2  
3 sub(pattern, repl, string, count=0, flags=0)
4 # pattern: 正则模型
5 # repl   : 要替换的字符串或可执行对象
6 # string : 要匹配的字符串
7 # count  : 指定匹配个数
8 # flags  : 匹配模式
技术分享
 # 与分组无关

        origin = "hello alex bcd alex lge alex acd 19"
        r = re.sub("a\w+", "999", origin, 2)
        print(r)
Demo

split

1 # split,根据正则匹配分割字符串
2  
3 split(pattern, string, maxsplit=0, flags=0)
4 # pattern: 正则模型
5 # string : 要匹配的字符串
6 # maxsplit:指定分割个数
7 # flags  : 匹配模式
技术分享
# 无分组
        origin = "hello alex bcd alex lge alex acd 19"
        r = re.split("alex", origin, 1)
        print(r)

        # 有分组
        
        origin = "hello alex bcd alex lge alex acd 19"
        r1 = re.split("(alex)", origin, 1)
        print(r1)
        r2 = re.split("(al(ex))", origin, 1)
        print(r2)
Demo
技术分享
IP:
^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$
手机号:
^1[3|4|5|8][0-9]\d{8}$
邮箱:
[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+
常用正则表达式

 

re模块

标签:key   pytho   pat   内容   多个   取出   code   which   log   

原文地址:http://www.cnblogs.com/shiluoliming/p/6395392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!