您好我如何从以下字符串
创建子字符串Netall_Low_Lin_kor_110_180
Netall_Low_Lin_cer_110_181
Netall_Low_Lin_asa_110_182
Netall_Low_Lin_row_110_183
Netall_Low_Lin_psq_182_42
Netall_Low_Lin_vyt_182_41
我希望以“Netall_Low_Lin_kor”为一部分,“110_180”为另一部分的方式拆分上述字符串。对于“Netall_Low_Lin_psq_182_42”,我想将其拆分为“Netall_Low_Lin_psq”和“182_42”。
有没有办法拆分这些字符串?
答案 0 :(得分:3)
list_of_strings = [
"Netall_Low_Lin_kor_110_180",
"Netall_Low_Lin_cer_110_181",
"Netall_Low_Lin_asa_110_182",
"Netall_Low_Lin_row_110_183",
"Netall_Low_Lin_psq_182_42",
"Netall_Low_Lin_vyt_182_41"
]
import re
pattern = re.compile("_(?=\d+_\d+)")
for current_string in list_of_strings:
print pattern.split(current_string)
<强>输出强>
['Netall_Low_Lin_kor', '110_180']
['Netall_Low_Lin_cer', '110_181']
['Netall_Low_Lin_asa', '110_182']
['Netall_Low_Lin_row', '110_183']
['Netall_Low_Lin_psq', '182_42']
['Netall_Low_Lin_vyt', '182_41']
答案 1 :(得分:2)
如果子串具有固定大小,则无需使用正则表达式,在这种情况下使用切片更简单:
s = 'Netall_Low_Lin_kor_110_180'
s[:18]
=> Netall_Low_Lin_kor
s[19:]
=> 110_180