re.MatchObject() Python
2015-12-02 11:17
615 查看
Match object对象有一个布尔值。可以通过if检查Mathch object是否匹配成功。Match object是re方法match()和seatch()返回的对象,没有匹配成功,返回None,否则True。group(),groups()方法返回Match object中的字符串:
<pre name="code" class="python">#pattern中的()用来分组 group(0)整个匹配到的match,group(1)为第一个,以此类推 >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist") >>> m.group(0) # The entire match 'Isaac Newton' >>> m.group(1) # The first parenthesized subgroup. 'Isaac' >>> m.group(2) # The second parenthesized subgroup. 'Newton' >>> m.group(1, 2) # Multiple arguments give us a tuple. ('Isaac', 'Newton')
#groups()将分组匹配到的字符串作为元祖返回,没有匹配到的分组默认None >>> m = re.match(r"(\d+)\.(\d+)", "24.1632") >>> m.groups() ('24', '1632')
>>> m = re.match(r"(\d+)\.?(\d+)?", "24") >>> m.groups() # Second group defaults to None. ('24', None) >>> m.groups('0') # Now, the second group defaults to '0'. ('24', '0')
相关文章推荐
- 基于SuperMap iObjects.NET 7C 中扩展图层实现航线一键查询系统
- #Paper Reading# Learning to Segment Object Candidates
- Qt多线程间信号槽传递非QObject类型对象的参数
- objective-c 委托的理解与应用
- Delphi组件开发-在窗体标题栏添加按钮(使用MakeObjectInstance(NewWndProc),并处理好多消息)
- 用Spring+Hibernate做项目时候遇到 java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit
- Cloneable接口和Object的clone()方法
- Objective-C 编码建议
- runtime之Associated Objects
- Objective-C:Objective-C 和 Core Foundation 对象相互转换的内存管理
- Object-C,数组NSArray
- Object-C,数组NSArray
- Object-C,数组NSArray
- How Do I Declare A Block in Objective-C? [备忘]
- How Do I Declare A Block in Objective-C? [备忘]
- object-c之UITableView数据的编辑
- object-c之UITableView数据的编辑
- object-c之UITableView的数据索引控件UISearchController
- object-c之UITableView的数据索引控件UISearchController
- NotSupportedException-无法将类型“System.DateTime”强制转换为类型“System.Object”