描述:将‘电信4-8,11区’转换成以下形式‘电信4区-电信5区-电信6区-电信7区-电信8区-电信11区’。
import re
string = input("请输入游戏区:" )
t1 = string.split(',')
str=re.findall(r"\D*",t1[0])[0]
b = []
for m in t1:
if('-' in m):
res = re.search(r'(\d{1,})-(\d{1,})',m)
b.extend(range(int(res.group(1)),int(res.group(2))+1))
else:
res = re.search(r'(\d{1,})',m)
b.append(res.group(1))
for e in b:
if e != b[-1]:
print('%s%s区-'%(str,e) ,end="")
else:
print('%s%s区'%(str,e) ,end="")