首页 > 解决方案 > 如何在python中使用正则表达式根据值打印界面的整行

问题描述

configuration="""
interface value is 3/40
    description eth/1-20-ugb-eth/58
    logging event port 
    ip address 34.5.62
    no downtine
interface value is 1/67
    description eth/4-56-ugb-eth/78
    logging event address
    ip address 5.7.8.9
    no downtime"""

预期输出:

interface value is 3/40
  description eth/1-20-ugb-eth/58
  logging event port 
  ip address 34.5.62
  no downtine

这里我需要根据我的值打印接口配置。如果值为 3/40,则需要打印接口的特定配置,但我正在获取所有接口行。任何人都可以帮助我解决这个问题。

我的代码:

re.search(
    r"\b{0}(.*)\b".format("interface value{}".format("3/40")) + r"downtime",
    configuration,
    re.DOTALL,
)

标签: pythonregex

解决方案


推荐阅读