re.search最速遍历
表示数字
https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6
from os import spawnl import re text = input() index = 0 result = '' while index < len(text): tmp = re.search('[0-9]+', text[index:]) # 注意切片导致索引变动 if tmp: result+=text[index:tmp.span()[0]+index] result+='*'+tmp.group()+'*' index = tmp.span()[1]+index else: result+=text[index:] index = len(text) print(result) # for i in range(len(text)): # print(f'in:i {i} index {index} result {result}') # if i < index: # continue # if re.search('[0-9]+', text[i:]): # # 找到数字段落 # tmp = re.search('[0-9]+', text[i:]) # # 返回数字段落 # result+=text[index:tmp.span()[0]-1] # # 加到该数字字段之前的非数字段落 # result+='*'+tmp.group()+'*' # # 修饰并加上数字段落 # index = tmp.span()[1] # # 索引设置到该数字段落之后 # elif i == len(text)-1: # # 已找不到数字段落 且已经到最后 # result+=text[index:] # # 加上累计的非数字段落 # index = i # # 索引设置到该段落之后 # print(f'out:i {i} index {index} result {result}') # print(result)