开发者

Python regex and splitting

开发者 https://www.devze.com 2023-04-09 04:50 出处:网络
I have a string like this mystr = \"K1_L1_L开发者_如何学运维2XX\" I want to break it to the following format

I have a string like this

mystr = "K1_L1_L开发者_如何学运维2  XX"

I want to break it to the following format

K1 L1 L2  XX

where K1, L1, L2 can be anything but have this format of a char followed by a number. I am doing this in python using the following regex:

a = "K1_L1_L2  XX"
re.split("[\c\d\_]+",a)

which gives me the following output

['K', 'L', 'L', '  ', '.', '']

but I want something like this

['K1', 'L1', 'L2', '  ', '.', '']

what is the possible workaround?


There are problems with the code you have included in your example above. I would edit them but I'm not 100% sure what you are looking for.

The following:

import re

a = "K1_L1_L2  XX"
print re.split("[ _]", a)

will print:

['K1', 'L1', 'L2', '', 'XX']


maybe

re.split("([A-Za-z]\d)",a)

?

0

精彩评论

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

关注公众号