Yui
Yui

Reputation: 13

separating numbers from letters; python

I want to separate numbers from letters. I tried it like this, but it won't work with negative numbers. What do i have to change?

match = re.match(r"([0-9]+(?:.[0-9]+)?)([a-z]+)", "-0.5m", re.I)
number = match.group(1)
letter = = match.group(2)

Upvotes: 0

Views: 242

Answers (1)

Wooble
Wooble

Reputation: 89897

match = re.match(r"(-?[0-9]+(?:.[0-9]+)?)([a-z]+)", "-0.5m", re.I)

Adds -? to optionally allow a minus sign at the start of the number.

Upvotes: 3

Related Questions