Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why re2 result different from re module in Python?

Tags:

python

regex

re2

I try to use re2.

import re
print re.search('cde', 'abcdefg').group(0)

Result:

cde

But re2 result is different

import re2
print re2.search('cde', 'abcdefg').group(0)

Result:

1
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
  1. Why re2 output number 1 when every execution new string pattern?
  2. How to hide the number 1?
  3. Why the result is different with re module (not found => return None)?

The re2 version is 0.2.20. and Python is 2.7

Thank you

like image 816
Puffin GDI Avatar asked Jul 26 '13 03:07

Puffin GDI


1 Answers

This is a bug of version 0.2.20. See this issue or this one. You'd better clone the source from github and then install it. Don't install it via pip.

like image 142
zhangyangyu Avatar answered Nov 08 '22 06:11

zhangyangyu