Forward slash in a Python regex


Question

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?

1
15
10/29/2009 1:56:41 PM

Accepted Answer

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.

22
11/25/2017 5:49:20 AM

You are saying it to replace zero or more slashes with 'a'. So it does replace each "no character" with 'a'. :)

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon