[issue39949] truncating match in regular expression match objects repr

Seth Troisi report at bugs.python.org
Thu Mar 12 17:49:27 EDT 2020


New submission from Seth Troisi <braintwo at gmail.com>:

Following on https://bugs.python.org/issue17087

Today I was mystified by why a regex wasn't working.

    >>> import re
    >>> re.match(r'.{10}', 'A'*49+'B')
    <_sre.SRE_Match object; span=(0, 10), match='AAAAAAAAAA'>

    >>> re.match(r'.{49}', 'A'*49+'B')
    <_sre.SRE_Match object; span=(0, 49), match='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>

    >>> re.match(r'.{50}', 'A'*49+'B')
    <_sre.SRE_Match object; span=(0, 50), match='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>

I became confused on why the B wasn't matching in the third example; It is matching just
in the interactive debugger it doesn't fit on the line and doesn't show


My suggestion would be to truncate match (in the repr) and append '...' when it's right quote wouldn't show


with short matches (or exactly enough space) there would be no change

    >>> re.match(r'.{48}', string.ascii_letters)
    <_sre.SRE_Match object; span=(0, 48), match='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV'>

when not all of match can be displayed

    >>> re.match(r'.{49}', string.ascii_letters)
    <_sre.SRE_Match object; span=(0, 49), match='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW>
    <_sre.SRE_Match object; span=(0, 49), match='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS'...>


I'm happy to help out by writing tests or impl if folks thing this is a good idea.

I couldn't think of other examples (urllib maybe?) in Python of how this is handled but I could potentially look for some if that would help

----------
components: Library (Lib)
messages: 364052
nosy: Seth.Troisi, serhiy.storchaka
priority: normal
severity: normal
status: open
title: truncating match in regular expression match objects repr
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39949>
_______________________________________


More information about the Python-bugs-list mailing list