78

我想拆分这样的字符串

'foofo21'
'bar432'
'foobar12345'

进入

['foofo', '21']
['bar', '432']
['foobar', '12345']

有人知道在 python 中执行此操作的简单方法吗?

4

10 回答 10

74

我将通过re.match以下方式使用来解决此问题:

import re
match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I)
if match:
    items = match.groups()
print(items)
>> ("foofo", "21")
于 2009-01-09T23:12:01.137 回答
53
>>> def mysplit(s):
... 头 = s.rstrip('0123456789')
... 尾 = s[len(head):]
...返回头部,尾部
...
>>> [mysplit(s) for s in ['foofo21', 'bar432', 'foobar12345']]
[('foofo', '21'), ('bar', '432'), ('foobar', '12345')]
>>>
于 2009-01-10T06:17:25.597 回答
31

另一种选择:

>>> [re.split(r'(\d+)', s) for s in ('foofo21', 'bar432', 'foobar12345')]
[['foofo', '21', ''], ['bar', '432', ''], ['foobar', '12345', '']]
于 2009-01-10T00:54:09.243 回答
28
>>> r = re.compile("([a-zA-Z]+)([0-9]+)")
>>> m = r.match("foobar12345")
>>> m.group(1)
'foobar'
>>> m.group(2)
'12345'

因此,如果您有一个具有该格式的字符串列表:

import re
r = re.compile("([a-zA-Z]+)([0-9]+)")
strings = ['foofo21', 'bar432', 'foobar12345']
print [r.match(string).groups() for string in strings]

输出:

[('foofo', '21'), ('bar', '432'), ('foobar', '12345')]
于 2009-01-09T23:12:16.533 回答
11

我总是提出 findall() =)

>>> strings = ['foofo21', 'bar432', 'foobar12345']
>>> [re.findall(r'(\w+?)(\d+)', s)[0] for s in strings]
[('foofo', '21'), ('bar', '432'), ('foobar', '12345')]

请注意,我使用的正则表达式比以前的大多数答案更简单(键入更少)。

于 2009-01-09T23:40:54.463 回答
7

这是一个从任意长度的字符串中分隔多个单词和数字的简单函数,re 方法仅分隔前两个单词和数字。我认为这将在未来对其他人有所帮助,

def seperate_string_number(string):
    previous_character = string[0]
    groups = []
    newword = string[0]
    for x, i in enumerate(string[1:]):
        if i.isalpha() and previous_character.isalpha():
            newword += i
        elif i.isnumeric() and previous_character.isnumeric():
            newword += i
        else:
            groups.append(newword)
            newword = i

        previous_character = i

        if x == len(string) - 2:
            groups.append(newword)
            newword = ''
    return groups

print(seperate_string_number('10in20ft10400bg'))
# outputs : ['10', 'in', '20', 'ft', '10400', 'bg'] 
于 2019-08-05T13:44:37.413 回答
3

不使用正则表达式,使用 isdigit() 内置函数,仅当起始部分是文本且后部分是数字时才有效

def text_num_split(item):
    for index, letter in enumerate(item, 0):
        if letter.isdigit():
            return [item[:index],item[index:]]

print(text_num_split("foobar12345"))

输出 :

['foobar', '12345']
于 2019-04-25T06:27:21.067 回答
2
import re

s = raw_input()
m = re.match(r"([a-zA-Z]+)([0-9]+)",s)
print m.group(0)
print m.group(1)
print m.group(2)
于 2015-11-19T19:28:53.407 回答
0

这是该问题的简单解决方案,无需regex

user = input('Input: ') # user = 'foobar12345'
int_list, str_list = [], []

for item in user:
 try:
    item = int(item)  # searching for integers in your string
  except:
    str_list.append(item)
    string = ''.join(str_list)
  else:  # if there are integers i will add it to int_list but as str, because join function only can work with str
    int_list.append(str(item))
    integer = int(''.join(int_list))  # if you want it to be string just do z = ''.join(int_list)

final = [string, integer]  # you can also add it to dictionary d = {string: integer}
print(final)
于 2019-09-22T18:27:59.483 回答
0

对于字符串中有多个随机放置的数字的情况,这有点长,但更通用。此外,它不需要导入。

def getNumbers( input ):
    # Collect Info
    compile = ""
    complete = []

    for letter in input:
        # If compiled string
        if compile:
            # If compiled and letter are same type, append letter
            if compile.isdigit() == letter.isdigit():
                compile += letter
            
            # If compiled and letter are different types, append compiled string, and begin with letter
            else:
                complete.append( compile )
                compile = letter
            
        # If no compiled string, begin with letter
        else:
            compile = letter
        
    # Append leftover compiled string
    if compile:
        complete.append( compile )
    
    # Return numbers only
    numbers = [ word for word in complete if word.isdigit() ]
        
    return numbers
于 2020-10-08T12:38:35.930 回答