[Tutor] Fwd: findall() returns tuples or string?
eryksun
eryksun at gmail.com
Thu Mar 7 07:35:44 CET 2013
On Thu, Mar 7, 2013 at 12:26 AM, suhas bhairav <suhasbhairav at hotmail.com> wrote:
> As far as my understanding goes, re.findall without using "()" inside your
> pattern will return you a list of strings when you search something similar
> to the one shown below:
>
> a="Bangalore, India"
> re.findall("[\w][\n]*",a)
With respect to using groups with findall, there are 3 cases discussed
in the docs:
http://docs.python.org/2/library/re#re.findall
>>> a = "Bangalore, India\n\n\n"
>>> re.findall(r'\w+\s*', a) # no groups
['Bangalore', 'India\n\n\n']
>>> re.findall(r'(\w+)\s*', a) # 1 group
['Bangalore', 'India']
>>> re.findall(r'(\w+)(\s*)', a) # 2+ groups
[('Bangalore', ''), ('India', '\n\n\n')]
CPython 2.7.3, _sre.c, pattern_findall (see 2089-2116):
http://hg.python.org/cpython/file/70274d53c1dd/Modules/_sre.c#l2088
More information about the Tutor
mailing list