re.match('(...),..',string)返回什么?

时间:2014-06-09 06:59:24

标签: python regex match

我是python的新手并且有一个混乱:

type = order_change_separate_t

re.match('(...)..', type).group(1)将返回什么?它会返回order_change_separate吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

你应该使用python的交互式shell来尝试这些东西:

sgupta-3:~ sgupta$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> type = "order_change_separate_t"
>>> import re
>>> print re.match('(...)..', type).group(1)
ord

打印'ord'

(...)是由捕获组定义的组1,3个点匹配ord。

print re.match('(.*)..', type).group(1)

将返回'order_change_separate'

以上显然是一种非常粗略且不可扩展的方法,因为它只适用于'order_change_separate',后面只有2个字符。

更好的方法是使用量词作为上面提到的用户'aelor'。

答案 1 :(得分:0)

它将返回ord

因为capture group ()

中只有三个字符

如果你想order_change_separate使用它:

re.match('(.*?)_.$', type).group(1)

这将匹配最后underscore followed by a character

之前的所有内容
相关问题