0

I run into a problem where I need to capture all groups in a Kleene plus closure. I tried with another scenario and got the same behavior. So an example

(?:([A-Za-z]+) )+

for the string

This is a sentence.

I always get back the last match as a group instead of all the possible groups. In the case above, group(1) is a and there are no other groups. I would want group(1) to be This group(2) to be is and group(3) a. For the above I was using search

EDIT

Serves me right for not using the exact case I care about ...

\{([ A-Z]+)(?:\|([ A-Z]+))+\}

and

NBAR -> { AP NBAR | NBAR PP | VPG | N | N N }

In this case, findall does not work. Using this online tool I get [(u' AP NBAR ', u' N N ')] which is quite similar as to what groups() would give me.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
demongolem
  • 9,474
  • 36
  • 90
  • 105
  • 1
    Python don't have the \G feature which allow this kind of captures. But you can find alternatives here: http://stackoverflow.com/questions/529830/do-python-regexes-support-something-like-perls-g – Casimir et Hippolyte May 01 '13 at 04:12

1 Answers1

2
re.findall("[A-Za-z]+", sentence)
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405