Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 Regex Syntax Warning #634

Open
cdillond opened this issue Mar 11, 2024 · 0 comments
Open

Python3 Regex Syntax Warning #634

cdillond opened this issue Mar 11, 2024 · 0 comments

Comments

@cdillond
Copy link

cdillond commented Mar 11, 2024

 * Xed version - Version 3.4.5
 * Distribution - Fedora 39

Issue
Opening xed from the command line results in the following warning:

usr/lib64/xed/plugins/open-uri-context-menu/open-uri-context-menu.py:33: SyntaxWarning: invalid escape sequence '\?'
RE_URI_RFC2396 = re.compile("((([a-zA-Z][0-9a-zA-Z+\\-\\.]*):)?/{0,2}([0-9a-zA-Z;:,/\?@&=\+\$\.\-_!~\*'\(\)%]+))?(#[0-9a-zA-Z;,/\?:@&\=+$\.\\-_!~\*'\(\)%]+)?")

Steps to reproduce
Run xed from the command line.

Expected behaviour

No warning.

Other information
The python re library documentation notes:

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. Also, 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 string literal in this line
RE_URI_RFC2396 = re.compile("((([a-zA-Z][0-9a-zA-Z+\\-\\.]*):)?/{0,2}([0-9a-zA-Z;:,/\?@&=\+\$\.\-_!~\*'\(\)%]+))?(#[0-9a-zA-Z;,/\?:@&\=+$\.\\-_!~\*'\(\)%]+)?")
should instead be a raw string. The \\-\\ part also seems redundant to me, so the line could instead be:
RE_URI_RFC2396 = re.compile(r"((([a-zA-Z][0-9a-zA-Z+\\.]*):)?/{0,2}([0-9a-zA-Z;:,/\?@&=\+\$\.\-_!~\*'\(\)%]+))?(#[0-9a-zA-Z;,/\?:@&\=+$\.\\-_!~\*'\(\)%]+)?")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant