4 回答

TA贡献1804条经验 获得超7个赞
text = "71B46>TC>77"
position = text.index('>')
result = text[0:position]
# 71B46
更新
import re
regex = r"[\W+]"
text = "71B46>TC>77"
position = re.search(regex, text).start()
print(text[0:position])
# 71B46

TA贡献1813条经验 获得超2个赞
一线解决方案
import re
get_chars_before_spl_chars = lambda string1 :re.findall("[\dA-Za-z]*",string1)[0]
get_chars_before_spl_chars('71B46>TC>77')
#'71B46'

TA贡献2016条经验 获得超9个赞
你应该试试这个。
import re
start_pos = re.search(r'\W+', string1).start()
print(string1[0:start_pos])

TA贡献1786条经验 获得超13个赞
不使用正则表达式。过滤字母数字字符。
s = '71B46>TC>77'
modified = ''.join(filter(str.isalnum, s))
# or
modified = ''.join((c for c in s if c.isalnum()))
添加回答
举报