【问题标题】:Having trouble with the following python regex以下 python 正则表达式有问题
【发布时间】: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 标志。

regex101link

我的正则表达式的问题是它从文本的开头捕获,所以它捕获了所有内容。 如何制作正则表达式,使其不捕获以下文本:

   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 的设备的供应商特定文本输出。

标签: python regex


【解决方案1】:

您可以将此正则表达式与否定字符类一起使用:

test\s\d{1,10}\s[^\n]+new\s+description.+?no\sshutdown\s+exit
#               ^^^^^^  [^\n] matches anything but line break 

确保您的正则表达式使用 DOTALL 或 s 模式。

RegEx Demo

在您的正则表达式中,.* 匹配任何字符,包括换行符,因为您使用的是 s 模式,因此它在块中匹配太多。另一方面,[^\n] 仅在同一行内匹配。

【讨论】:

  • 嗨 Anubhava,感谢您的建议解决了我的问题,虽然我不明白 [^n] 是如何工作的。但是我已经编辑了我的帖子,你会看到我遇到了另一个问题,其中一个块有很多没有关闭语句,我想捕获所有这些。请参阅更新后的帖子,请知道我如何捕捉这种情况。
  • 我已经在回答中解释了[^\n] 匹配除换行符之外的任何内容。
  • 嗨 Anubhava,只是检查我的理解以确保我理解正则表达式及其工作原理,对不起,我是正则表达式的新手并试图了解它是如何工作的。我对您的建议的理解如下:这个位本身是 vprn\s[^\n]+,它匹配 vprn 文字,然后是空格字符,然后我们说不匹配换行符,但使用+ 量词,一直匹配到第一行结束。因此,当您说 [^\n] 仅在同一行内匹配时,我不明白这如何跳转到下一行,即 \s+description.+?等等
  • 是的,您的理解是正确的。 \s+description.+? 跨行匹配,因为使用 sDOTALL 模式强制 .(点)匹配任何字符,包括换行符。
  • test\s\d{1,10}\s[^\n]+\s+描述,\s+如何允许跳转到下一行? \s 匹配任何空白字符并且 + 表示一个或多个。 \s 与 re.DOTALL 标志一起使用时是否有特殊含义?我可以理解那个描述。+?,这个。跨换行符匹配,但在此之前我们只使用 \s 和 no 。运算符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-20
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多