您好,如何从以下字符串创建子字符串
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”。
有什么办法可以分割这些字符串吗?
最佳答案
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']
关于python - 如何从 python 中的给定字符串创建子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970878/