【发布时间】:2020-10-31 05:31:25
【问题描述】:
您好,我有以下类型的配置文件,我正在尝试在 Python 中使用正则表达式进行解析。 你会看到以 test 开头的行出现了两次,我在第二个实例之后,它有两个不同的关键字,description 和 no shutdown。
test 68068070 same "random-text" someone 68068070 new
int "random-test" new
exit
int "somemore-random-text" new
exit
exit
test 58698496 name "58698496" someone 1 new
interface "some random text" new
exit
exit
test 4035849058 name "can be any random text" someone 76060600 new
int "another random text" new
exit
exit
test 806406450 name "random text goes here" someone 89899 new
exit
test 68068070 same "random-text" someone 68068070 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 806406450 name "random text goes here" someone 89899 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 58698496 name "58698496" someone 1 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 58698496 name "58698496" someone 1 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 4035849058 name "can be any random text" someone 76060600 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
从上面的文字中,我只想捕获以下实例:
test 68068070 same "random-text" someone 68068070 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 806406450 name "random text goes here" someone 89899 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 58698496 name "58698496" someone 1 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 58698496 name "58698496" someone 1 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
test 4035849058 name "can be any random text" someone 76060600 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
我在 regex101 尝试过的正则表达式如下:
test\s\d{1,10}\s.+?new\s+description.*?no\sshutdown\s*exit
开启 gs 标志。
我的正则表达式的问题是它从文本的开头捕获,所以它捕获了所有内容。 如何制作正则表达式,使其不捕获以下文本:
test 68068070 same "random-text" someone 68068070 new
int "random-test" new
exit
int "somemore-random-text" new
exit
exit
test 58698496 name "58698496" someone 1 new
interface "some random text" new
exit
exit
test 4035849058 name "can be any random text" someone 76060600 new
int "another random text" new
exit
exit
test 806406450 name "random text goes here" someone 89899 new
exit
我希望我的匹配从以下文本开始,然后返回此类型的任何类似项目:
test 68068070 same "random-text" someone 68068070 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
no shutdown
exit
有关如何调整或更改正则表达式以解决此问题的任何帮助。 我曾尝试使用负前瞻,即不匹配 int 甚至是正前瞻来查找描述,但这些都没有成功。 谢谢
我遇到的另一个问题如下:
在某些情况下,在单个实例中多次没有关闭,例如:
test 68068070 same "random-text" someone 68068070 new
description "random-text-here"
Lots of random text goes here
no shutdown
exit
exit
Lots of random text goes here
no shutdown
exit
exit
Lots of random text goes here
no shutdown
exit
exit
Lots of random text goes here
no shutdown
exit
exit
no shutdown
exit
exit
我如何一直选择这种情况以持续不关机。在这个情况下,正则表达式出现并在第一次不关机时停止。 所以这个正则表达式有效:
test\s\d{1,10}\s[^\n]+new\s+description.+?no\sshutdown\s+exit
但在第一次没有关闭时停止。任何想法我如何去到最后一次不关机。
【问题讨论】:
-
regex 不是解析格式化多行嵌套数据格式的好选择。
-
您好,有什么想法可以用来提取我需要的文本。不幸的是,这是来自不是 JSON 或 XML 的设备的供应商特定文本输出。