python - Pyparsing 缩进和去缩进
问题描述
我正在解析路由器的配置文件。通常我将正则表达式与命名组一起使用,为此目的: https : //regex101.com/r/EcyPeh/5 在此示例中,我使用“svcindent”命名组+关键字“exit”作为服务配置上下文的缩进。
如何用 pyparsing 做同样的事情?
以下源代码中有SkipTo(intend + Keyword("exit")。我试图将它用作de-indent。但它匹配各种数量的空格。这就是它匹配stp上下文的“exit”关键字的原因.
from pyparsing import *
data = """
vpls 82 customer 700 i-vpls create
service-mtu 9040
send-flush-on-failure
propagate-mac-flush
stp
shutdown
exit
sap 2/2/8:0.1 create
ingress
qos 13
queue-override
queue 1 create
cbs 1000
rate 10240 cir 10240
exit
exit
exit
exit
spoke-sdp 4:82 endpoint "vpls-82-uplink" create
stp
shutdown
exit
precedence primary
no shutdown
exit
spoke-sdp 6:82 endpoint "vpls-82-uplink" create
stp
shutdown
exit
no shutdown
exit
mac-move
no shutdown
exit
no shutdown
exit
vpls 121 customer 700 i-vpls create
service-mtu 9040
send-flush-on-failure
propagate-mac-flush
stp
shutdown
exit
sap 2/2/9:900 create
ingress
qos 13
queue-override
queue 1 create
cbs 3000
rate 10240 cir 10240
exit
exit
exit
exit
spoke-sdp 4:121 endpoint "vpls-121-uplink" create
stp
shutdown
exit
precedence primary
no shutdown
exit
spoke-sdp 6:121 endpoint "vpls-121-uplink" create
stp
shutdown
exit
no shutdown
exit
mac-move
no shutdown
exit
no shutdown
exit
vpls 1986692 customer 555 i-vpls create
service-mtu 9040
send-flush-on-failure
propagate-mac-flush
stp
shutdown
exit
sap 2/2/9:130 create
ingress
qos 13
queue-override
queue 1 create
cbs 1000
rate 10240 cir 10240
exit
exit
exit
exit
spoke-sdp 4:1986692 endpoint "vpls-1986692-uplink" create
stp
shutdown
exit
precedence primary
no shutdown
exit
spoke-sdp 6:1986692 endpoint "vpls-1986692-uplink" create
stp
shutdown
exit
no shutdown
exit
mac-move
no shutdown
exit
no shutdown
exit
"""
indent = LineStart() + OneOrMore(White(' ', exact=4))
deident = matchPreviousExpr(indent) + Keyword("exit")
svc_id = Word(nums)
customer_id = Word(nums)
sp = White(' ', exact=1)
svc_stmt = Combine(indent("int_s") + Keyword("vpls") + sp + svc_id + sp + Keyword("customer") + sp + customer_id + SkipTo(Keyword("create"), include=True)) + Combine(SkipTo(deident, include=True))
svc_stmt.leaveWhitespace()
parseTree = svc_stmt.searchString(data)
parseTree.pprint()
Result:
[[' vpls 82 customer 700 i-vpls create',
'\n'
' service-mtu 9040\n'
' send-flush-on-failure\n'
' propagate-mac-flush\n'
' stp\n'
' shutdown\n'
' exit'],
[' vpls 121 customer 700 i-vpls create',
'\n'
' service-mtu 9040\n'
' send-flush-on-failure\n'
' propagate-mac-flush\n'
' stp\n'
' shutdown\n'
' exit'],
[' vpls 1986692 customer 555 i-vpls create',
'\n'
' service-mtu 9040\n'
' send-flush-on-failure\n'
' propagate-mac-flush\n'
' stp\n'
' shutdown\n'
' exit']]
解决方案
推荐阅读
- javascript - Next.js 不会在生产运行时构建新图像
- r - Vroom/fread 不会读取 LARGE .csv 文件 - 无法对其进行内存映射
- php - 如何修复 WordPress 中的错误:“标签必须配对,没有开始标签:[]”?
- linux - Bash 如何根据大小移动文件夹
- visual-studio - 更改 Workspace Visual Sudio 2019 的计算机名称
- linux - 为什么名称是accept4 而不是accept1,就像epoll_create1 for epoll_create 一样?
- python - 在Python中过滤具有条件的对象列表
- 32bit-64bit - 当 32 位用户在 64 位上时如何处理 ioctl 调用
- javascript - 为什么我的 Wordpress Timber 组件会破坏页面?
- c++ - 将许多图像加载到 OpenGL 中并将它们渲染到屏幕上