[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gnulib-tool.py: Simplify regular expressions.
From: |
Collin Funk |
Subject: |
Re: gnulib-tool.py: Simplify regular expressions. |
Date: |
Thu, 11 Apr 2024 13:24:41 -0700 |
User-agent: |
Mozilla Thunderbird |
On 4/11/24 12:25 PM, Bruno Haible wrote:
> Oh, this means that r'[x\$]' contains dollar and backslash, whereas the
> programmer might have thought that it contains only the dollar? Indeed,
> it's worth to listen to these warnings!
I don't think it changes the meaning:
import re
re.match(r'[x$]*', 'x\\$').group()
'x'
re.match(r'[x\$]*', 'x\\$').group()
'x'
re.match(r'[x\\$]*', 'x\\$').group()
'x\\$'
Since in Python you have to backslash a backslash in string literals.
Therefore to backslash a backslash you would have to do '\\\\'. Since
we are using raw strings a backslashed backslash is r'\\'. The Python
Regular expression page has a far better explanation of that [1]. :)
# Regular string.
re.match('\\\\', '\\').group()
'\\'
print(re.match('\\\\', '\\').group())
\
# Raw string.
re.match(r'\\', '\\').group()
'\\'
print(re.match(r'\\', '\\').group())
\
[1] https://docs.python.org/3/library/re.html#module-re
Collin