Python Regular Expression Matches

I have a question about regular expressions. When using orconstruct

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> for mo in re.finditer('a|ab', 'ab'):
...     print mo.start(0), mo.end(0)
... 
0 1

we get only one match, which is expected as the first left branch to be accepted. My question is, is this possible and how to build a regular expression that will give both (0,1) and (0,2). As well as how to do this for any regular expression in the form r1 | r2 | ... | rn.

Similarly, is it possible to achieve this for structures *, +and ?? Default:

>>> for mo in re.finditer('a*', 'aaa'):
...     print mo.start(0), mo.end(0)
... 
0 3
3 3
>>> for mo in re.finditer('a+', 'aaa'):
...     print mo.start(0), mo.end(0)
... 
0 3
>>> for mo in re.finditer('a?', 'aaa'):
...     print mo.start(0), mo.end(0)
... 
0 1
1 2
2 3
3 3

The second question is why the empty lines coincide at the ends, but not elsewhere, as in the case of *and ??

EDIT:

, , : @mgilson, re.finditer , , , (), . , Python.

, Python , . .

EDIT2:

Perl. . @Qtax .

+5
2

, . docs re.finditer:

, MatchObject RE

( )


, , , , ​​- , finditer > , (. ; -).

+1

, Perl, :

(?:a|ab)(?{ say $& })(?!)

(?{ code }) , . , . (?!) , regex ..

.

:

perl -E "$_='ab'; /(?:a|ab)(?{ say $& })(?!)/"

:

a
ab

:

perl -E "$_='aaaa'; /a+(?{ say $& })(?!)/"

:

aaaa
aaa
aa
a
aaa
aa
a
aa
a
a
+1

All Articles