Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何提取MAC id?_Python_Regex_String - Fatal编程技术网

Python 如何提取MAC id?

Python 如何提取MAC id?,python,regex,string,Python,Regex,String,我需要从一组相关命令中提取MAC id 从第一个命令开始,在第二个命令中应用括号eth-1(2-04)内的值(2,4),以从第二个命令的输出中提取MAC id 如何提取第二个命令输出中与2 4在同一行的MAC id s1 = '''100 int-maint debug(2-02) eth-10(2-00) maint(4-03) 101 bear-test eth-1(2-04) eth-2(2-07) 102 int-test eth-4(2-06) eth-7

我需要从一组相关命令中提取MAC id

从第一个命令开始,在第二个命令中应用括号
eth-1(2-04)
内的值(2,4),以从第二个命令的输出中提取MAC id

如何提取第二个命令输出中与2 4在同一行的MAC id

s1 = '''100 int-maint      debug(2-02) eth-10(2-00) maint(4-03)
101 bear-test      eth-1(2-04) eth-2(2-07)
102 int-test      eth-4(2-06) eth-7(2-05) eth-8(2-03) eth-9(2-01) gig-3(2-08) eth-3(4-02) eth-5(4-00) eth-6(4-01) gig-1(4-08) gig-2(4-10) poe-1(4-04) poe-2(4-05) poe-3(4-06) poe-4(4-07)
'''
# output of second command

r2 = '''
     2     4   101  00:80:a3:bf:72:d4      5
     2    10   101  00:e0:4b:52:56:56      7
     2    10   100  02:00:00:64:00:00      7
     2    10   102  02:00:00:66:00:00      6
     2     0   100  94:10:3e:b9:4f:5c      2
Switch  Port   FID      MAC-Address    Aging
     4     3   100  56:1a:5e:a2:4a:73      7
'''


import re
s1o = (next((x for x in s1.split() if 'eth-1(2-04)' in x), None))
print(s1o)
print(r2.split())
到目前为止,我已经得到了输出(如下所示)。我不确定拆分()是否是将其分解以提取此值的最佳方法00:80:a3:bf:72:d4

eth-1(2-04)
['2', '4', '101', '00:80:a3:bf:72:d4', '5', '2', '10', '101', '00:e0:4b:52:56:56', '7', '2', '10', '100', '02:00:00:64:00:00', '7', '2', '10', '102', '02:00:00:66:00:00', '6', '2', '0', '100', '94:10:3e:b9:4f:5c', '2', 'Switch', 'Port', 'FID', 'MAC-Address', 'Aging', '4', '3', '100', '56:1a:5e:a2:4a:73', '7']


预期结果:00:80:a3:bf:72:d4

对于第一个命令,您可以应用此模式
eth-1\(\d)-0(\d)

eth-1(2-04)
['2', '4', '101', '00:80:a3:bf:72:d4', '5', '2', '10', '101', '00:e0:4b:52:56:56', '7', '2', '10', '100', '02:00:00:64:00:00', '7', '2', '10', '102', '02:00:00:66:00:00', '6', '2', '0', '100', '94:10:3e:b9:4f:5c', '2', 'Switch', 'Port', 'FID', 'MAC-Address', 'Aging', '4', '3', '100', '56:1a:5e:a2:4a:73', '7']

eth-1\(
=按字面意思匹配
eth-1

(\d)
-匹配数字并将其存储在第一个CAPTRUNG组中

-0
-按字面意思匹配
-0

(\d)
-匹配数字并将其存储在第二个CAPTRUNG组中

现在,您可以创建另一个正则表达式来应用于第二个命令:
2\s+4\s++\d++\s+([^\s]+)

2
-按字面意思匹配2,这来自第一个正则表达式的第一个捕获组

\s+
-匹配一个或多个空格

4
-按字面意思匹配4,这来自第一个正则表达式的第二个捕获组

\s+
-匹配一个或多个空格

\d+
-匹配一个或多个数字

\s+
-匹配一个或多个空格

([^\s]+)
-匹配除空格以外的一个或多个字符并将其存储在捕获组中,这将捕获您的mac地址


我也是python初学者,我认为这段代码对您有帮助

101 bear-test      eth-1(2-04) eth-2(2-07)
102 int-test      eth-4(2-06) eth-7(2-05) eth-8(2-03) eth-9(2-01) gig-3(2-08) eth-3(4-02) eth-5(4-00) eth-6(4-01) gig-1(4-08) gig-2(4-10) poe-1(4-04) poe-2(4-05) poe-3(4-06) poe-4(4-07)
'''
# output of second command

r2 = '''
     2     4   101  00:80:a3:bf:72:d4      5
     2    10   101  00:e0:4b:52:56:56      7
     2    10   100  02:00:00:64:00:00      7
     2    10   102  02:00:00:66:00:00      6
     2     0   100  94:10:3e:b9:4f:5c      2
Switch  Port   FID      MAC-Address    Aging
     4     3   100  56:1a:5e:a2:4a:73      7
'''


import re
s1o = (next((x for x in s1.split() if 'eth-1(2-04)' in x), None))
print(s1o)
base_list=(r2.split())
base_str=",".join(base_list)
pattren=r"(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2})"
all_mac=re.findall(pattren,base_str)
print(all_mac)
print('###########################################')
for i in all_mac:
    result=re.sub(",",":",str(i))
    print(result)

我可以
grep'2 4'
(中间有一个选项卡)来获取行。
print([x.split()[3]用于r2中的x.split('\n')如果重新编译('^\s*2\s*4')。findall(x)]
这是你想要的吗?关于
re.findall('..:':':'r2')如何
?Bear Brown,你的代码运行得很好。有没有办法在[re.compile('^\s*2\s*4')]中将
2
4
作为变量传递?我必须从第一个命令中获取这些值。如果python3.6+,你可以使用format或f-string,例如,
print([x.split()[3]代表r2中的x.split('\n'),如果re.compile('^\s*{\s*}\s*}“.format(2,4)).findall(x)])
代码有一定的帮助。这和我想要实现的不完全一样。正则表达式应用的好例子。