0
re.findall(r"(i).*\1", "i am says i am")

The only thing that's returned is one "i". I've searched for hours w/o finding a solution to this problem.

UPDATE: I was hoping to return "i am says i".

martineau
  • 119,623
  • 25
  • 170
  • 301
Charles Saag
  • 611
  • 5
  • 20
  • 2
    What are you hoping will happen? The only _captured_ group is `(i)`. – jtbandes Dec 15 '17 at 18:06
  • Use `re.finditer`, you may access the whole match values easily. Something like `print([x.group(0) for x in re.finditer(pattern, txt)])` – Wiktor Stribiżew Dec 15 '17 at 18:13
  • 'EVz0EZCvFFczBvWZxYwdE0bkgyge10fjxXwDwV0XTbAbBvbDvUUXafyx0xfxh3jeUYFAByB0UYyTdwbccUjecvzg0i12AFdwTVaZbBccDxZTvFAyy1320d1iwWTd0WBDXTXbxxzT0WWgdbjayacyddhibaaYWdUFyFTyxwYzyvxdeivyxv232dgawhE0ExTdWYbxwFzEbwb12hdcevgvBVWwbv0BwTWzzXUXyETDWB21wxadvyieijab03AcFTvUVxVyFwc0AFXdcwXTAhyvjaijjh2vZ0ZDvEazWbdTEdFVZef22kefgycgvdxfeB0BCUCCdYyzTzxBh1he20efcx2' What I'm trying to do is capture the stings above where it begins w/a capital letter and ends w/the same capital letter. See example data above. – Charles Saag Dec 15 '17 at 18:19
  • @CharlesSaag: Then you can use `print re.search(r'([A-Z]).*\1', str).group(0)` (see my answer below) – anubhava Dec 15 '17 at 19:02

2 Answers2

1

When using parenthesis (...) in a regex, only things inside them are captured, so in your case, only "i"s are found.

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
1

That's the way findall works, it will print capturing groups if they are present.

You can use re.search:

>>> print re.search(r'(i).*\1', "i am says i am").group(0)
i am says i

Or use additional grouping in findall as this:

>>> print re.findall(r'((i).*\2)', "i am says i am")[0][0]
i am says i
anubhava
  • 761,203
  • 64
  • 569
  • 643