字符串操作

输入字符串:123#@hello#a111

输出:['123', '#@', 'hello', '#', 'a', '111']

说明:按照 数字,字符,字母进行提取

import re

chars = '123#@hello#a111'
new = []
while len(chars) > 0:
    if chars[0].isdigit():
        res = re.match(r'\d+', chars).group()
        n = len(res)
        new.append(res)
        chars = chars[n:]
    elif chars[0].isalpha():
        res = re.match(r'[a-zA-Z]+', chars).group()
        n = len(res)
        new.append(res)
        chars = chars[n:]
    else:
        res = re.match(r'[^a-zA-Z0-9]+', chars).group()
        n = len(res)
        new.append(res)
        chars = chars[n:]

print(new)

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐