On 5/16/11 11:25 AM, Tracubik wrote:
pls help me fixing this:

import re
s = "linka la baba"
re_s = re.compile(r'(link|l)a' , re.IGNORECASE)

print re_s.findall(s)

output:
['link', 'l']

why?
i want my re_s to find linka and la, he just find link and l and forget
about the ending a.

can anyone help me? trying the regular expression in redemo.py (program
provided with python to explore the use of regular expression) i get what
i want, so i guess re_s is ok, but it still fail...
why?

The parentheses () create a capturing group, which specifies that the contents of the group should be extracted. See the "(...)" entry here:

  http://docs.python.org/library/re#regular-expression-syntax

You can use the non-capturing version of parentheses if you want to just isolate the | from affecting the rest of the regex:

"""
(?:...) A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
"""

[~]
|1> import re

[~]
|2> s = "linka la baba"

[~]
|3> re_s = re.compile(r'(?:link|l)a' , re.IGNORECASE)

[~]
|4> print re_s.findall(s)
['linka', 'la']


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to