Federer
Federer

Reputation: 34735

Regex to separate Numeric from Alpha

I have a bunch of strings:

"10people"
"5cars"
..

How would I split this to?

['10','people']
['5','cars']

It can be any amount of numbers and text.

I'm thinking about writing some sort of regex - however I'm sure there's an easy way to do it in Python.

Upvotes: 4

Views: 1546

Answers (7)

PaulMcG
PaulMcG

Reputation: 63719

Piggybacking on jsbueno's idea, using str.translate, followed by split:

import string

allchars = ''.join(chr(i) for i in range(32,256))
digExtractTrans = string.maketrans(allchars, ''.join(ch if ch.isdigit() else ' ' for ch in allchars))
alpExtractTrans = string.maketrans(allchars, ''.join(ch if ch.isalpha() else ' ' for ch in allchars))

data = "5people10cars"
numbers = data.translate(digExtractTrans).split()
names = data.translate(alpExtractTrans).split()

You only need to create the translation tables once, then call translate and split as often as you want.

Upvotes: 0

kennytm
kennytm

Reputation: 523294

Use the regex (\d+)([a-zA-Z]+).

import re
a = ["10people", "5cars"]
[re.match('^(\\d+)([a-zA-Z]+)$', x).groups() for x in a]

Result:

[('10', 'people'), ('5', 'cars')]

Upvotes: 8

Anonymous
Anonymous

Reputation: 50339

In general, a split on /(?<=[0-9])(?=[a-z])|(?<=[a-z])(?=[0-9])/i separates a string that way.

Upvotes: 2

jsbueno
jsbueno

Reputation: 110301

If you are like me and goes long loops around to avoid regexpes justbecause they are ugly, here is a non-regex approach:

data = "5people10cars"

numbers = "".join(ch if ch.isdigit() else "\n" for ch in data).split()
names = "".join(ch if not ch.isdigit() else "\n" for ch in data).split()

final = zip (numbers, names)

Upvotes: 0

Dominic Rodger
Dominic Rodger

Reputation: 99761

>>> import re
>>> s = '10cars'
>>> m = re.match(r'(\d+)([a-z]+)', s)
>>> print m.group(1)
10
>>> print m.group(2)
cars

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798656

>>> re.findall('(\d+|[a-zA-Z]+)', '12fgsdfg234jhfq35rjg')
['12', 'fgsdfg', '234', 'jhfq', '35', 'rjg']

Upvotes: 8

YOU
YOU

Reputation: 123831

>>> re.findall("\d+|[a-zA-Z]+","10people")
['10', 'people']

>>> re.findall("\d+|[a-zA-Z]+","10people5cars")
['10', 'people', '5', 'cars']

Upvotes: 3

Related Questions