3

I am creating a metric measurement converter. The user is expected to enter in an expression such as 125km (a number followed by a unit abbreviation). For conversion, the numerical value must be split from the abbreviation, producing a result such as [125, 'km']. I have done this with a regular expression, re.split, however it produces unwanted item in the resulting list:

import re
s = '125km'
print(re.split('(\d+)', s))

Output:

['', '125', 'km']

I do not need nor want the beginning ''. How can I simply separate the numerical part of the string from the alphabetical part to produce a list using a regular expression?

6
  • Are you going to run into stuffs like kg*(m^2)/(s^2)? Commented Feb 3, 2015 at 2:49
  • @nhahtdh Most likely not. Commented Feb 3, 2015 at 2:50
  • @nhahtdh, surely J or Nm would suffice for that :-) Commented Feb 3, 2015 at 2:53
  • 1
    @paxdiablo: Sure, but not as simple for m/s^2 (acceleration). Commented Feb 3, 2015 at 2:57
  • 1
    @Jacob: Unit for energy, J or kg*(m^2)/(s^2), or N*m. It is also equivalent to W*h, which is used to measure electrical consumption (usually kW*h, kilowatt hour). Commented Feb 3, 2015 at 2:58

2 Answers 2

13

What's wrong with re.findall ?

>>> s = '125km'
>>> re.findall(r'[A-Za-z]+|\d+', s)
['125', 'km']

[A-Za-z]+ matches one or more alphabets. | or \d+ one or more digits.

OR

Use list comprehension.

>>> [i for i in re.split(r'([A-Za-z]+)', s) if i]
['125', 'km']
>>> [i for i in re.split(r'(\d+)', s) if i]
['125', 'km']
Sign up to request clarification or add additional context in comments.

2 Comments

What if the number had decimals, is there a way to handle that case? say 1.25km how can I get ['125', 'km']
re.findall(r'[A-Za-z]+|\d+(?:\.\d+)?', s)
1

Split a string into list of sub-string (number and others)

Using program:

s = "125km1234string"
sub = []
char = ""
num = ""
for letter in s:
    if letter.isdigit():
        if char:
            sub.append(char)
            char = ""
        num += letter
    else:
        if num:
            sub.append(num)
            num = ""
        char += letter
sub.append(char) if char else sub.append(num)
print(sub)

Output

['125', 'km', '1234', 'string']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.