Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70611 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2014-04-25 14:32 -0400 |
| Last post | 2014-04-26 07:24 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: possible bug in re expression? Terry Reedy <tjreedy@udel.edu> - 2014-04-25 14:32 -0400
Re: possible bug in re expression? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-26 07:24 +0000
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-04-25 14:32 -0400 |
| Subject | Re: possible bug in re expression? |
| Message-ID | <mailman.9507.1398450797.18130.python-list@python.org> |
On 4/25/2014 12:30 PM, Robin Becker wrote:
> Whilst translating some javascript code I find that this
>
> A=re.compile('.{1,+3}').findall(p)
>
> doesn't give any error, but doesn't manage to find the strings in p that
> I want len(A)==>0, the correct translation should have been
>
> A=re.compile('.{1,3}').findall(p)
>
> which works fine.
>
> should
>
> re.compile('.{1,+3}')
>
> raise an error? It doesn't on python 2.7 or 3.3.
And it should not because it is not an error. '+' means 'match 1 or more
occurrences of the preceding re' and the preceding re is ','.
>>> re.match('a{1,+3}', 'a{1,,,3}').group()
'a{1,,,3}'
I suppose that one could argue that '{' alone should be treated as
special immediately, and not just when a matching '}' is found, and
should disable other special meanings. I wonder what JS does if there is
no matching '}'?
--
Terry Jan Reedy
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-04-26 07:24 +0000 |
| Message-ID | <535b5f33$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #70611 |
On Fri, 25 Apr 2014 14:32:30 -0400, Terry Reedy wrote:
> On 4/25/2014 12:30 PM, Robin Becker wrote:
[...]
>> should
>>
>> re.compile('.{1,+3}')
>>
>> raise an error? It doesn't on python 2.7 or 3.3.
>
> And it should not because it is not an error. '+' means 'match 1 or more
> occurrences of the preceding re' and the preceding re is ','.
Actually, no. Braces have special meaning, and are used to specify a
number of matches. R{m,n} matches from m to n repetitions of the
preceding regex R:
py> re.search('(spam){2,4}', 'spam-spamspamspam-spam').group()
'spamspamspam'
This surprises me:
> >>> re.match('a{1,+3}', 'a{1,,,3}').group()
> 'a{1,,,3}'
I would have expected that either +3 would have been interpreted as just
"3", or that it would have been an invalid regex. It appears that what is
happening is that if the braces cannot be interpreted as a repetition
group, they are interpreted as regular characters. Those sort of silent
errors is why I hate programming in regexes.
> I suppose that one could argue that '{' alone should be treated as
> special immediately, and not just when a matching '}' is found, and
> should disable other special meanings. I wonder what JS does if there is
> no matching '}'?
Probably silently do the wrong thing :-)
--
Steven D'Aprano
http://import-that.dreamwidth.org/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web