python - 如何从 python 中的给定字符串创建子字符串?

标签 python regex

您好,如何从以下字符串创建子字符串

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']

RegEx101 Demo + Explanation

关于python - 如何从 python 中的给定字符串创建子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970878/

相关文章:

Python for 循环问题

c# - 将 Python SocketServer 与 C# 客户端连接

java - 检查字符串是否匹配特定的正则表达式

regex - PowerShell -replace 获取两个不同字符之间的字符串

javascript - 如何清理空格和新行?

python - 如何将确切的单词与正则表达式 python 匹配?

python - Scribus 专色 PDF

python - 在 Python 中更改对运行时函数的引用

python - 在 Python 中正确舍入 float 的最简单方法是什么?

regex - 什么正则表达式可以提取 perl 中除 <> 之外的所有数据?