MiscItems/2010-05-13

re 模块功能问深

问题

Zoom.Quiet <zoom.quiet@gmail.com>
sender-time     Sent at 09:48 (GMT+08:00). Current time there: 3:34 PM. ✆
to      "Python.cn@google" <python-cn@googlegroups.com>
date    Thu, May 13, 2010 at 09:48
subject re 模块功能问深

现有一正则表达式的技巧问题:

a="balalalalallalala"
b=re.compile("介个正则")
- b 的正则表达式 包含多个模式,或的关系;
- a 中可能有或是没有一个且仅一个模式,在b 中

"AB" -> 123423
"CD" -> 654623
"EF" -> 675647

当然俺,可以根据匹配的模式组编号另外再查个字典返回,但是,这就慢了...

re.sub()

小明同学 <wjm251@gmail.com>
sender-time     Sent at 14:42 (GMT-07:00). Current time there: 12:35 AM. ✆
reply-to        python-cn@googlegroups.com
to      python-cn@googlegroups.com
date    Thu, May 13, 2010 at 14:42

Toggle line numbers
   1 b = '(pat1|pat2|pat3)?'
   2 a = re.match(b)
   3 
   4 d = {None:val1,
   5     pat1:val2,
   6     pat2:val2,
   7     pat3:val3,
   8     #..........
   9    }
  10 
  11 return d[a.group()]

小明:实例

在下面的例子里,替换函数将十进制翻译成十六进制:

Toggle line numbers
   1 >>> def hexrepl( match ):
   2 ...     "Return the hex string for a decimal number"
   3 ...     value = int( match.group() )
   4 ...     return hex(value)
   5 ...
   6 >>> p = re.compile(r'\d+')
   7 >>> p.sub(hexrepl, 'Call 65490 for printing, 49152 for user code.')
   8 'Call 0xffd2 for printing, 0xc000 for user code.'

当使用模块级的 re.sub() 函数时,模式作为第一个参数。

阿暖

阿暖 <anuan2008@gmail.com>
sender-time     Sent at 16:40 (GMT+08:00). Current time there: 4:44 PM. ✆
reply-to        python-cn@googlegroups.com
to      python-cn@googlegroups.com
date    Thu, May 13, 2010 at 16:40

ply里见过有这样的写法

Toggle line numbers
   1 token={'123423':AB,
   2     '654623':"CD",
   3     '675647'  : "EF" ,
   4     }
   5 
   6 s='.**123423.**'
   7 m=re.match('''(123423)|(654623)|(675647)''',s)
   8 handle=token[m.group()]

的确靠谱 ;-)

使用 Kodos 测试 zoomq-2010-05-13-181627_365x663_scrot.png

自动生成的 Sample Code:

Toggle line numbers
   1 import re
   2 
   3 # common variables
   4 
   5 rawstr = r"""(?P<AB>123423)|(?P<CD>654623)|(?P<EF>675647)"""
   6 embedded_rawstr = r"""(?P<AB>123423)|(?P<CD>654623)|(?P<EF>675647)"""
   7 matchstr = """675645 123523 675647 675648 675649 99999999"""
   8 
   9 # method 1: using a compile object
  10 compile_obj = re.compile(rawstr)
  11 match_obj = compile_obj.search(matchstr)
  12 
  13 # method 2: using search function (w/ external flags)
  14 match_obj = re.search(rawstr, matchstr)
  15 
  16 # method 3: using search function (w/ embedded flags)
  17 match_obj = re.search(embedded_rawstr, matchstr)
  18 
  19 # Retrieve group(s) from match_obj
  20 all_groups = match_obj.groups()
  21 
  22 # Retrieve group(s) by index
  23 group_1 = match_obj.group(1)
  24 group_2 = match_obj.group(2)
  25 group_3 = match_obj.group(3)
  26 
  27 # Retrieve group(s) by name
  28 AB = match_obj.group('AB')
  29 CD = match_obj.group('CD')
  30 EF = match_obj.group('EF')

不用 token

Toggle line numbers
   1 # common variables
   2 rawstr = r"""(?P<AB>123423)|(?P<CD>654623)|(?P<EF>675647)"""
   3 matchstr = """675645 123523 675647 675648 675649 99999999"""
   4 
   5 # method 1: using a compile object
   6 compile_obj = re.compile(rawstr)
   7 match_obj = compile_obj.search(matchstr)
   8 print match_obj.groupdict()
   9 print [k for (k, v) in match_obj.groupdict().iteritems() if v !=None]


反馈

创建 by -- ZoomQuiet [2010-05-13 07:37:16]

MiscItems/2010-05-13 (last edited 2010-05-13 10:36:14 by ZoomQuiet)