找回密码
 注册
搜索
热搜: 超星 读书 找书
查看: 2437|回复: 2

[【计算机类】] python中re模块学习

[复制链接]
发表于 2008-9-28 10:55:32 | 显示全部楼层 |阅读模式
就个人而言,主要用它来做一些复杂字符串分析,提取想要的信息
学习原则:够用就行,需要的时候在深入

现总结如下:

正则表达式中特殊的符号:

\".\" 表任意字符
\"^ \" 表string起始
\"$\" 表string 结束
“*” “+” “?” 跟在字符后面表示,0个——多个, 1个——多个, 0个或者1个
*?, +?, ?? 符合条件的情况下,匹配的尽可能少//限制*,+,?匹配的贪婪性
{m} 匹配此前的字符,重复m次
{m,n} m到n次,m,n可以省略

举个例子 ‘a.*b’ 表示a开始,b结束的任意字符串
a{5} 匹配连续5个a

[] 表一系列字符 [abcd] 表a,b,c,d [^a] 表示非a
| A|B 表示A或者B , AB为任意的正则表达式 另外|是非贪婪的如果A匹配,则不找B
(…) 这个括号的作用要结合实例才能理解, 用于提取信息

\\d [0-9]
\\D 非 \\d
\\s 表示空字符
\\S 非空字符
\\w [a-zA-Z0-9_]
\\W 非 \\w

一:re的几个函数

1: compile(pattern, [flags])
  根据正则表达式字符串 pattern 和可选的flags 生成正则表达式 对象
  
  生成正则表达式 对象(见二)
  
  其中flags有下面的定义:
  I 表示大小写忽略
  L 使一些特殊字符集,依赖于当前环境     
  M 多行模式 使 ^ $ 匹配除了string开始结束外,还匹配一行的开始和结束
  S “.“ 匹配包括‘\\n’在内的任意字符,否则 . 不包括‘\\n’
  U Make \\w, \\W, \\b, \\B, \\d, \\D, \\s and \\S dependent on the Unicode character properties database  
  X 这个主要是表示,为了写正则表达式,更可毒,会忽略一些空格和#后面的注释
  
  其中S比较常用,
  应用形式如下
  import re
  re.compile(……,re.S)
  
2: match(pattern,string,[,flags])
  让string匹配,pattern,后面分flag同compile的参数一样
  返回MatchObject 对象(见三)
  
3: split( pattern, string[, maxsplit = 0])
  用pattern 把string 分开
  >>> re.split('\\W+', 'Words, words, words.')
  ['Words', 'words', 'words', '']
  括号‘()’在pattern内有特殊作用,请查手册
  
4:findall( pattern, string[, flags])
  比较常用,
  从string内查找不重叠的符合pattern的表达式,然后返回list列表
  
5:sub( pattern, repl, string[, count])
  repl可以时候字符串,也可以式函数
  当repl是字符串的时候,
  就是把string 内符合pattern的子串,用repl替换了
  
  当repl是函数的时候,对每一个在string内的,不重叠的,匹配pattern
  的子串,调用repl(substring),然后用返回值替换substring
  
  >>> re.sub(r'def\\s+([a-zA-Z_][a-zA-Z_0-9]*)\\s*\\(\\s*\\):',
  ...    r'static PyObject*\\npy_\\1(void)\\n{',
  ...    'def myfunc():')
  'static PyObject*\\npy_myfunc(void)\\n{'
  
  
  >>> def dashrepl(matchobj):
  ...   if matchobj.group(0) == '-': return ' '
  ...   else: return '-'
  >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
  'pro--gram files'

二:正则表达式对象 (Regular Expression Objects )
  
  产生方式:通过 re.compile(pattern,[flags])回
  
  match( string[, pos[, endpos]]) ;返回string[pos,endpos]匹配
  pattern的MatchObject(见三)
  
  split( string[, maxsplit = 0])
  findall( string[, pos[, endpos]])  
  sub( repl, string[, count = 0])
  这几个函数和re模块内的相同,只不过是调用形式有点差别
  
  re.几个函数和 正则表达式对象的几个函数,功能相同,但同一程序如果
  多次用的这些函数功能,正则表达式对象的几个函数效率高些
  
三:matchobject

  通过 re.match(……) 和 re.compile(……).match返回
  
  该对象有如下方法和属性:
  
  方法:
  group( [group1, ...])
  groups( [default])
  groupdict( [default])
  start( [group])
  end( [group])
  
  说明这几个函数的最好方法,就是举个例子
  
  matchObj = re.compile(r\"(?P<int>\\d+)\\.(\\d*)\")
  m = matchObj.match(&#39;3.14sss&#39;)
  #m = re.match(r\"(?P<int>\\d+)\\.(\\d*)\", &#39;3.14sss&#39;)
  
  print m.group()
  print m.group(0)
  print m.group(1)
  print m.group(2)
  print m.group(1,2)
  
  print m.group(0,1,2)
  print m.groups()
  print m.groupdict()
  
  print m.start(2)  
  print m.string

输出如下:  
  3.14        
  3.14        
  3         
  14         
  (&#39;3&#39;, &#39;14&#39;)   
  (&#39;3.14&#39;, &#39;3&#39;, &#39;14&#39;)
  (&#39;3&#39;, &#39;14&#39;)   
  {&#39;int&#39;: &#39;3&#39;}   
  2         
  3.14sss
  
  所以group() 和group(0)返回,匹配的整个表达式的字符串
  另外group(i) 就是正则表达式中用第i个“()” 括起来的匹配内容
  (&#39;3.14&#39;, &#39;3&#39;, &#39;14&#39;)最能说明问题了。
回复

使用道具 举报

发表于 2008-9-28 12:15:59 | 显示全部楼层
总结得挺不错,有几个稍显马虎的地方,要是细读一遍再发也许对初学者的帮助更大。

作为一个最小集合,您的总结里漏掉了一个重要的函数,search。它和match的区别是,它在字符串内部进行滑动匹配,而match是和整个字符串进行匹配。findall无法作为替代品,因为它仅仅给出了匹配内容的集合,缺少MatchObject的具体信息。命名上似乎值得注意,Python的search相当于其它一些语言里的match,如Perl和.NET的Regex。Boost的regex和xpressive库都和Python的概念一致,分为regex_match和regex_search。
回复

使用道具 举报

发表于 2008-9-28 18:31:51 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|网上读书园地

GMT+8, 2024-5-11 19:31 , Processed in 0.286027 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表