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

A escape character is used when a REGEXP is an argument of "strip" string function #65482

Closed
TitoBouzout mannequin opened this issue Apr 17, 2014 · 4 comments
Closed
Labels
type-bug An unexpected behavior, bug, or error

Comments

@TitoBouzout
Copy link
Mannequin

TitoBouzout mannequin commented Apr 17, 2014

BPO 21283
Nosy @ericvsmith, @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 = None
closed_at = <Date 2014-04-17.14:50:33.441>
created_at = <Date 2014-04-17.13:54:45.391>
labels = ['type-bug', 'invalid']
title = 'A escape character is used when a REGEXP is an argument of "strip" string function'
updated_at = <Date 2014-04-21.13:30:18.872>
user = 'https://bugs.python.org/TitoBouzout'

bugs.python.org fields:

activity = <Date 2014-04-21.13:30:18.872>
actor = 'Tito.Bouzout'
assignee = 'none'
closed = True
closed_date = <Date 2014-04-17.14:50:33.441>
closer = 'eric.smith'
components = []
creation = <Date 2014-04-17.13:54:45.391>
creator = 'Tito.Bouzout'
dependencies = []
files = []
hgrepos = []
issue_num = 21283
keywords = []
message_count = 4.0
messages = ['216687', '216688', '216694', '216944']
nosy_count = 4.0
nosy_names = ['eric.smith', 'ezio.melotti', 'mrabarnett', 'Tito.Bouzout']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21283'
versions = ['Python 3.3']

@TitoBouzout
Copy link
Mannequin Author

TitoBouzout mannequin commented Apr 17, 2014

Hello!

I got a report that the character "\" was removed from a string using the following code

"\\server\path\to".strip(r'"\'<>')

At first insight, looks like a bug, because I don't expect the use of the escape character at all. Then I noticed, that our mistake there is that the "strip" argument should be a "string" not a REGEXP.

Kinda weird to read, and I've no idea if this is expected behaviour in Python, as I'm relatively very new. So just informing,

Kind regards,

--
Tito

@TitoBouzout TitoBouzout mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Apr 17, 2014
@mrabarnett
Copy link
Mannequin

mrabarnett mannequin commented Apr 17, 2014

The argument isn't a regex, it's a raw string literal consisting of the characters " (quote), \ (backslash), ' (apostrophe), < (less than) and > (greater than).

@ericvsmith
Copy link
Member

In addition, you probably want "\\server\path\to" to be a raw string, too. That way, the backslashes are not given special meaning. Notice the difference in output between these two:

>>> "\\server\path\to".strip(r'"\'<>') 
'server\\path\to'
>>> r"\\server\path\to".strip(r'"\'<>') 
'server\\path\\to'

In the first one, '\t' is being treated as a tab character, in the second one you see a backslash followed by a 't'.

My rule of thumb is: any time you have a string with a filename containing backslashes, you want it to be a raw string.

@TitoBouzout
Copy link
Mannequin Author

TitoBouzout mannequin commented Apr 21, 2014

Thanks guys for the information! Is still weird to me that the escape character is used, but well ! Will remember this bug.
Kind regards,

@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
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant