简体   繁体   中英

python how to split string with more than one character?

I would like to split a string as below

1234ABC into 123 and ABC

2B into 2 and B

10E into 10 and E

I found split function does not work because there is no delimiter

You can use itertools.groupby with boolean isdigit function.

from itertools import groupby

test1 = '123ABC'
test2 = '2B'
test3 = '10E'

def custom_split(s):
    return [''.join(gp) for _, gp in groupby(s, lambda char: char.isdigit())]

for t in [test1, test2, test3]:
    print(custom_split(t))

# ['123', 'ABC']
# ['2', 'B']
# ['10', 'E']

This can quite easily be accomplished using the re module:

>>> import re
>>> 
>>> re.findall('[a-zA-Z]+|[0-9]+', '1234ABC')
['1234', 'ABC']
>>> re.findall('[a-zA-Z]+|[0-9]+', '2B')
['2', 'B']
>>> re.findall('[a-zA-Z]+|[0-9]+', '10E')
['10', 'E']
>>> # addtionall test case
... 
>>> re.findall('[a-zA-Z]+|[0-9]+', 'abcd1234efgh5678')
['abcd', '1234', 'efgh', '5678']
>>> 

The regex use is very simple. Here is quick walk through:

  • [a-zA-Z]+ : Match one or more alphabetic characters lower case or upper
  • | or...
  • [0-9]+ : One or more whole numbers

Another way to solve it using re package

r = re.search('([0-9]*)([a-zA-Z]*)', test_string)
r.groups()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM