import re
# test strings
examples = [
'0001_Smoke_B_B',
'0002_Smoke_B_B',
'0012_Smoke_B_B',
'MA103',
'MA104',
'00_00MA105'
]
for text in examples:
result = re.sub('^(\d+|_)*', '', text, flags=re.IGNORECASE)
print(text, '->', result)
# example from other answers and comments
for text in examples:
result = re.sub('^[\d+_]+', '', text, flags=re.IGNORECASE)
print(text, '->', result)
如果删除
.
,它将使用*
搜索所有数字和_
测试代码
我还从其他答案/评论中添加了
'^[\d+_]+'
用于替换的正则表达式
^[\d_]+
^
这将查找字符串的开头[\d_]
带有数字或下划线的字符数组+
1次或更多次Regex101
相关问题 更多 >
编程相关推荐