python_koans icon indicating copy to clipboard operation
python_koans copied to clipboard

Fix invalid escape sequences in regex by using raw string literals

Open stabbotco1 opened this issue 11 months ago • 0 comments

Problem

When running contemplate_koans.py on Python 3.8+, the following warnings appear:

❯ python3 contemplate_koans.py /Users/user1/projects/python_koans/runner/sensei.py:63: SyntaxWarning: invalid escape sequence '\d' m = re.search("(?<= line )\d+" ,err) /Users/user1/projects/python_koans/runner/sensei.py:149: SyntaxWarning: invalid escape sequence '\w' m = re.search("^ \w(\w)+.*$",line)

These warnings occur because \d and \w are special escape sequences in regular expressions, and Python expects them to be inside a raw string literal (r"pattern") or properly escaped ("\d"). Fix

Converted the affected regex patterns into raw string literals (r"pattern") to prevent Python from misinterpreting escape sequences:

Before (causing warnings):

m = re.search("(?<= line )\d+" ,err) m = re.search("^ \w(\w)+.*$",line)

After (fixed with raw strings):

m = re.search(r"(?<= line )\d+" ,err) m = re.search(r"^ \w(\w)+.*$",line)

Impact

Removes SyntaxWarning messages in Python 3.8+.
Improves regex compatibility across different Python versions.
No changes to logic or functionality.

Let me know if any additional changes are needed! 🚀

stabbotco1 avatar Feb 20 '25 13:02 stabbotco1