如何在python中仅使用正则表达式方法获得整个输出?

如何在python中仅使用正则表达式方法获得整个输出?,python,regex,python-3.x,Python,Regex,Python 3.x,我希望每个界面细节都作为输出,但我只得到了前4行。有人能告诉我这个输出背后的概念是什么吗。是仅使用re方法编写此程序的任何其他方法 你的例子稍微修改了一下 >import re int_br=""" Vlan1 unassigned YES NVRAM up Vlan2 unassigned YES unset up

我希望每个界面细节都作为输出,但我只得到了前4行。有人能告诉我这个输出背后的概念是什么吗。是仅使用re方法编写此程序的任何其他方法

你的例子稍微修改了一下

>import re

int_br="""
Vlan1                  unassigned      YES NVRAM  up                        
Vlan2                  unassigned      YES unset  up                        
Vlan3                  unassigned      YES unset  up                        
Vlan4                  unassigned      YES unset  up                        
Vlan5                  unassigned      YES unset  down                        
Vlan6                  unassigned      YES unset  up                        
Vlan7                  unassigned      YES unset  up                        
Vlan8                  unassigned      YES unset  up                        
Vlan9                  unassigned      YES unset  up                        
Vlan10                 unassigned      YES unset  up                        
Vlan11                 unassigned      YES unset  up                        
Vlan12                 unassigned      YES unset  down                        
Vlan13                 unassigned      YES unset  up                        
Vlan14                 unassigned      YES unset  up                        
Vlan15                 unassigned      YES unset  up                        
Vlan16                 unassigned      YES unset  up                        
Vlan17                 unassigned      YES unset  up                        
Vlan18                 unassigned      YES unset  down"""
>a=list(int_br.split('\n'))
>i=1
>for l in a:
    if (re.search(r'(up|UP)',a[i])):
        print(a[i])
    i=i+1

您应该按照josifoski写的那样做,因为不必要地使用变量“i”使代码变得复杂

无论如何,代码中的错误实际上是您将语句 (i=i+1)在“if”块内,该块应在“if”块外,在“for”块内,如下所示,因为无论行是否包含(up | up),“i”都应增加

import re
a=int_br.split('\n')
for l in a:
    if (re.search(r'(up|UP)', l)):
        print(l)

请关注编程基础知识。祝你好运

对于int\br.splitliines()中的l:
会更好,对吗?代码更少,这可能会导致问题:为什么python而不是AWKC可以告诉我这是编写正则表达式脚本的方法,还是我们可以编写更高效的脚本。我认为josifoski的代码就足够了
for l in a:
    if (re.search(r'(up|UP)',a[i])):
        print(a[i])
    i=i+1