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

Invalid expression error if a regex ends with a backslash #57618

Closed
mlesniew mannequin opened this issue Nov 15, 2011 · 2 comments
Closed

Invalid expression error if a regex ends with a backslash #57618

mlesniew mannequin opened this issue Nov 15, 2011 · 2 comments
Assignees
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@mlesniew
Copy link
Mannequin

mlesniew mannequin commented Nov 15, 2011

BPO 13409
Nosy @ezio-melotti

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/ezio-melotti'
closed_at = <Date 2011-11-15.19:16:18.103>
created_at = <Date 2011-11-15.19:01:05.153>
labels = ['expert-regex', 'type-bug', 'invalid']
title = 'Invalid expression error if a regex ends with a backslash'
updated_at = <Date 2011-11-15.19:16:18.102>
user = 'https://bugs.python.org/mlesniew'

bugs.python.org fields:

activity = <Date 2011-11-15.19:16:18.102>
actor = 'ezio.melotti'
assignee = 'ezio.melotti'
closed = True
closed_date = <Date 2011-11-15.19:16:18.103>
closer = 'ezio.melotti'
components = ['Regular Expressions']
creation = <Date 2011-11-15.19:01:05.153>
creator = 'mlesniew'
dependencies = []
files = []
hgrepos = []
issue_num = 13409
keywords = []
message_count = 2.0
messages = ['147696', '147697']
nosy_count = 2.0
nosy_names = ['ezio.melotti', 'mlesniew']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue13409'
versions = ['Python 2.7']

@mlesniew
Copy link
Mannequin Author

mlesniew mannequin commented Nov 15, 2011

If a regular expression ends with a backslash, an exception is raised. Of course, the backslash has to be escaped. The simplest example, that causes the error is a regular expression, that should match only a single backslash:

    import re
    r = re.compile("\\")

@mlesniew mlesniew mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Nov 15, 2011
@ezio-melotti
Copy link
Member

You either have to double escape it (once for python and then for the regex engine) or use raw strings:
>>> re.match("\\\\", '\\').group()
'\\'
>>> re.match(r"\\", '\\').group()
'\\'

@ezio-melotti ezio-melotti self-assigned this Nov 15, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-regex type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant