开发者

Regex to separate Numeric from Alpha

开发者 https://www.devze.com 2022-12-18 06:26 出处:网络
I have a bunch of strings: \"10people\" \"5cars\" .. How would I split this to? [\'10\',\'people\'] [\'5\',\'cars\']

I have a bunch of strings:

"10people"
"5cars"
..

How would I split this to?

['10','people']
['5','cars']
开发者_Python百科

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.


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


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')]


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

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


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


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


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号