Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward slash in a Python regex

Tags:

I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that [\w\d\s+-/*]* would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals that forward slashes are the culprit. For example:

>>> import re >>> re.sub(r'[/]*', 'a', 'bcd') 'abacada' 

Apparently forward slashes match between characters (even when it is in a character class, though only when the asterisk is present). Back slashes do not escape them. I've hunted for a while and not found any documentation on it. Any pointers?

like image 234
exupero Avatar asked Oct 29 '09 13:10

exupero


People also ask

How do you handle forward slash in regex?

You can escape it by preceding it with a \ (making it \/ ), or you could use new RegExp('/') to avoid escaping the regex.

Is forward slash a special character in regex?

A slash symbol '/' is not a special character, but in JavaScript, it is used to open and close the RegEx: /... pattern.../ , so we should escape it too.

Does forward slash need to be escaped in regex?

/Delimiters/ Delimiters have an impact on escaping: if the delimiter is / and the regex needs to look for / literals, then the forward slash must be escaped before it can be a literal ( \/ ).

How do you escape special characters in regex Python?

Python supports Regex via module re . Python also uses backslash ( \ ) for escape sequences (i.e., you need to write \\ for \ , \\d for \d ), but it supports raw string in the form of r'...' , which ignore the interpretation of escape sequences - great for writing regex.


1 Answers

Look here for documentation on Python's re module.

I think it is not the /, but rather the - in your first character class: [+-/] matches +, / and any ASCII value between, which happen to include the comma.

Maybe this hint from the docs help:

If you want to include a ']' or a '-' inside a set, precede it with a backslash, or place it as the first character.

like image 70
Ber Avatar answered Sep 19 '22 16:09

Ber