khaverim
khaverim

Reputation: 3554

Mystery undesired split-by-period in python re.split()

I have a string cleanFunc e.g.

cleanFunc = "3.04x^2.48"

When I use

terms = re.split("[+-/*^()]+",cleanFunc)

The result for terms is

terms: ['3', '04x', '2', '48']

I don't have a period . anywhere in the regex and for some reason the function is splitting when it scans a period. How can I avoid this? Any flags I can add to explicitly avoid splitting by periods? I've already tried to avoid removing periods by the following regex:

terms = re.split("(?<!\d)[.](?!\d)|[+-/*^()]+",cleanFunc)

Upvotes: 2

Views: 235

Answers (1)

Befall
Befall

Reputation: 6810

The reason is your '-' sign in there, which denotes 'from a - b' in ASCII characters, which would include periods, presumably.

Change your regex to escape it, like this:

terms = re.split(r"[+\-/*^()]+", cleanFunc)

and it works how you want it to.

Upvotes: 4

Related Questions