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?
Look here for documentation on Python's
I think it is not the
/, but rather the
- in your first character class:
/ 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.
You are saying it to replace zero or more slashes with
'a'. So it does replace each "no character" with
You probably meant
[/]+, i.e. one or more slashes.
EDIT: Read Ber's answer for a solution to the original problem. I didn't read the whole question carefully enough.