Skip to content

Fix invalid escape sequences in regex by using raw string literals #294

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stabbotco1
Copy link

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! 🚀

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

Successfully merging this pull request may close these issues.

1 participant