Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10441 > unrolled thread

unexpected regexp behaviour using 'A|B|C.....'

Started byAlienBaby <matt.j.warren@gmail.com>
First post2011-07-28 02:56 -0700
Last post2011-07-28 12:57 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  unexpected regexp behaviour using 'A|B|C.....' AlienBaby <matt.j.warren@gmail.com> - 2011-07-28 02:56 -0700
    Re: unexpected regexp behaviour using 'A|B|C.....' Peter Otten <__peter__@web.de> - 2011-07-28 12:57 +0200

#10441 — unexpected regexp behaviour using 'A|B|C.....'

FromAlienBaby <matt.j.warren@gmail.com>
Date2011-07-28 02:56 -0700
Subjectunexpected regexp behaviour using 'A|B|C.....'
Message-ID<6c92b791-58d2-4ea5-8997-48ef21ce69f8@z7g2000vbp.googlegroups.com>
When using re patterns of the form 'A|B|C|...'  the docs seem to
suggest that once any of A,B,C.. match, it is captured and no further
patterns are tried.  But I am seeing,

st='  Id Name                    Prov Type  CopyOf              BsId
Rd -Detailed_State-    Adm     Snp      Usr VSize'

p='Type *'
re.search(p,st).group()
'Type  '

p='Type *|  *Type'
re.search(p,st).group()
' Type'


Shouldn’t the second search return the same as the first, if further
patterns are not tried?

The documentation appears to suggest the first match should be
returned, or am I misunderstanding?

'|'
A|B, where A and B can be arbitrary REs, creates a regular expression
that will match either A or B. An arbitrary number of REs can be
separated by the '|' in this way. This can be used inside groups (see
below) as well. As the target string is scanned, REs separated by '|'
are tried from left to right.

 When one pattern completely matches, that branch is accepted. This
means that once A matches, B will not be tested further, even if it
would produce a longer overall match.

In other words, the '|' operator is never greedy. To match a literal
'|', use \|, or enclose it inside a character class, as in [|].

[toc] | [next] | [standalone]


#10444

FromPeter Otten <__peter__@web.de>
Date2011-07-28 12:57 +0200
Message-ID<mailman.1566.1311850649.1164.python-list@python.org>
In reply to#10441
AlienBaby wrote:

> When using re patterns of the form 'A|B|C|...'  the docs seem to
> suggest that once any of A,B,C.. match, it is captured and no further
> patterns are tried.  But I am seeing,
> 
> st='  Id Name                    Prov Type  CopyOf              BsId
> Rd -Detailed_State-    Adm     Snp      Usr VSize'
> 
> p='Type *'
> re.search(p,st).group()
> 'Type  '
> 
> p='Type *|  *Type'
> re.search(p,st).group()
> ' Type'
> 
> 
> Shouldn’t the second search return the same as the first, if further
> patterns are not tried?
> 
> The documentation appears to suggest the first match should be
> returned, or am I misunderstanding?

All alternatives are tried at a given starting position in the string before 
the algorithm advances to the next position. The second alternative 
"  *Type", at least one space followed by the character sequence "Type" 
matches right after "Prov" in  your example, therefore the first 
alternative, "Type" and any following spaces, which would match after 
"Prov " is never tried. 

Maybe you accidentally typed one extra " "? If you didn't " +Type" would be 
clearer.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web