Code improvement question

Thomas Passin list1 at tompassin.net
Fri Nov 17 10:17:37 EST 2023


On 11/17/2023 9:46 AM, Peter J. Holzer via Python-list wrote:
> On 2023-11-17 07:48:41 -0500, Thomas Passin via Python-list wrote:
>> On 11/17/2023 6:17 AM, Peter J. Holzer via Python-list wrote:
>>> Oh, and Python (just like Perl) allows you to embed whitespace and
>>> comments into Regexps, which helps readability a lot if you have to
>>> write long regexps.
>>>
> [...]
>>>>>> re.findall(r'\b[0-9]{2,7}-[0-9]{2}-[0-9]{2}\b', txt)
>>>
>>> \b         - a word boundary.
>>> [0-9]{2,7} - 2 to 7 digits
>>> -          - a hyphen-minus
>>> [0-9]{2}   - exactly 2 digits
>>> -          - a hyphen-minus
>>> [0-9]{2}   - exactly 2 digits
>>> \b         - a word boundary.
>>>
>>> Seems quite straightforward to me. I'll be impressed if you can write
>>> that in Python in a way which is easier to read.
>>
>> And the re.VERBOSE (also re.X) flag can always be used so the entire
>> expression can be written line-by-line with comments nearly the same
>> as the example above
> 
> Yes. That's what I alluded to above.

I know, and I just wanted to make it explicit for people who didn't know 
much about Python regexes.



More information about the Python-list mailing list