Vom: Mon, 21 Oct 2024 14:57:11 +0100

> On Mon, 21 Oct 2024 at 13:26, Graham Inggs <gin...@debian.org> wrote:
> 
> > invalid escape sequence '\d'  
> 
> (many times)
> 
> As far as I am aware \d is a valid escape character in Python regex.
> These look like false-positives (given that as far as I know all the
> code mentioned is working.)
> 

Haven't looked into this special case but it looks like mixing python string 
escape sequences [1] with regex special characters/special sequences [2].


Then the solution would be to use raw strings:  r"(\d+)" instead of "(\d+)"


From the docs:

"Regular expressions use the backslash character ('\') to indicate special 
forms or to allow special characters to be used without invoking their special 
meaning. This collides with Python’s usage of the same character for the same 
purpose in string literals; for example, to match a literal backslash, one 
might have to write '\\\\' as the pattern string, because the regular 
expression must be \\, and each backslash must be expressed as \\ inside a 
regular Python string literal.

[...]

please note that any invalid escape sequences in Python’s usage of the 
backslash in string literals now generate a SyntaxWarning and in the future 
this will become a SyntaxError. This behaviour will happen even if it is a 
valid escape sequence for a regular expression.

[...]

The solution is to use Python’s raw string notation for regular expression 
patterns; backslashes are not handled in any special way in a string literal 
prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', 
while "\n" is a one-character string containing a newline."



[1] https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences
[2] https://docs.python.org/3/library/re.html#index-9

Best regards
-- 
Daniel

Reply via email to