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?
You can escape it by preceding it with a \ (making it \/ ), or you could use new RegExp('/') to avoid escaping the 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.
/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 ( \/ ).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With