用于python中的多行的正则表达式

时间:2017-08-30 00:55:02

标签: python regex

我是regexp的新手,想知道当列的内容不相似时如何获取第3列值

链接中的O / P图片以便更好地理解

enter image description here

sw1-tb100#sh mod

Mod Ports Module-Type Model Status

1 34 1/10 / 40G IPS,2/4/8/10 / 16G FC模块DS-X9334-K9确定

2 48 1/10 Gbps以太网模块DS-X9848-480K9确定

5 0 Supervisor Module-3 DS-X97-SF1-K9有效*

2 个答案:

答案 0 :(得分:0)

你能发布表的txt格式吗?

re.findall("(\d{1,2}?) +(\d{1,2}?) +(.*?) +(.+?\s\w+\w+?) +(.*\-?) +(\w+)", "your_input_file")

以上内容将匹配您的前两行:

==========================================================
1 34 1/10/40G IPS,2/4/8/10/16G FC Module DS-X9334-K9 ok

2 48 1/10 Gbps Ethernet Module DS-X9848-480K9 ok
==========================================================

您也可以参考以下链接

python regular expression across multiple lines

答案 1 :(得分:0)

您可以使用简单的字符串拆分操作尝试以下操作,而不是正则表达式。

print " ".join(str2.replace("*","").split()[2:-2])
print " ".join(str2.split("DS")[0].split()[2:])

你可以将它放在一个循环中,为所有行重复它。

编辑1:

您可以使用以下任一方法删除星号问题。然后,如果日志中还有其他特殊情况,则可能需要对其进行调整。

std::vector<int> x({1,2,3});
int size=x.size():
x.erase(x.begin());
for(int i=0;i<size;i++)
    std::cout << x[i] << ' ';