很难匹配多行的正则表达式。我试过几次,但运气不好。
第一次尝试: ((?:\b#显示)(?:*\n?){6})
结果:失败。发现线可以在5-8之间的任何地方,有时更少或更多。所以匹配6次是行不通的。
第二次尝试:(?<=#\n)(显示*?版本)
结果:失败:不匹配任何东西,尽管我在其他匹配中使用了类似的正则表达式。
字符串我正在尝试匹配.
wgb-car1# show startup-config
Using 6149 out of 32768 bytes
!
! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user
!
version 12.4
no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!我正在努力匹配从显示到版本号的所有内容.
这个正则表达式适用于(?s)# show(.*)版本的,但我不知道如何获得数字,因为它们可以是小数的任意组合,但总是数字。
发布于 2017-03-06 21:02:00
您可以使用以下正则表达式:
(?s)#\sshow\s*(.*?)version\s*([\d.]+)巨蟒( python,演示)
import re
s = """wgb-car1# show startup-config
Using 6149 out of 32768 bytes
!
! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user
!
version 12.4
no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!"""
r = r"(?s)#\sshow\s*(.*?)version\s*([\d.]+)"
o = [m.group() for m in re.finditer(r, s)]
print o发布于 2017-03-06 21:02:23
尝试将换行符匹配到版本号,然后再不匹配换行符。您可以使用(?sm:show.*\nversion)获取多行行为(带有(?sm:...)设置),然后再使用类似于.*$的非多行操作。
发布于 2017-03-06 21:11:53
其中一个答案是使用pos。展望:
\#\ show
([\s\S]+?)
(?=version)作为完整的Python示例:
import re
string = """
wgb-car1# show startup-config
Using 6149 out of 32768 bytes
!
! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user
!
version 12.4
no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!"""
rx = re.compile(r'''
\#\ show
([\s\S]+?)
(?=version)
''', re.VERBOSE)
matches = [match.group(0) for match in rx.finditer(string)]
print(matches)
# ['# show startup-config\nUsing 6149 out of 32768 bytes\n!\n! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user\n!\n']https://stackoverflow.com/questions/42635393
复制相似问题