正则表达式查找文本块,当以特定文本开头但不包含该文本时。

huangapple go评论45阅读模式
英文:

regex find text block when it start with but does not contain

问题

在处理我们从路由器接收的日志时,我遇到了一个问题。实际上,这些日志只是我们从网络提供商那里收到的脚本控制台输出。我们已经能够处理一些块,但现在我遇到了正则表达式的另一个问题。这是日志的示例。

一些路由器没有所有块,在这种情况下,我想找到在“show run vpn 0”和下一个路由器名称之间的块,这是正则表达式。

(?<=show run vpn 0)(?s)(.*?)(?=PADB067)

但我不希望它从“show run vpn 0 | i weight”开始,那是另一个数据块(在这种情况下不存在)。所以归根结底,我正在寻找正确的正则表达式,它会在“show run vpn 0”后找到文本块,当行不包括“weight”时,直到“PADB067”。

顶,Harry

英文:

While processing the logs we receive from our routers I'v encountered a problem.
In fact these logs are just scripted console outputs we receive from our network provider.
We have heen able to process some blocks but now I'm encoutering another problem with regex.
This is a sample of the log

   PADB067# show vrrp
    % No entries found.
    PADB067# show run vpn 0  | i weight
    PADB067# show run vpn 0 
    vpn 0
     name Transport
     dns 4.2.2.2 secondary
     dns 8.8.8.8 primary
     host vbond-list ip x.x.x.x y.y.y.y z.z.z.z
     interface ge0/0
      description           &quot;ORCH=NETWORK - To INTERNET&quot;
.....
      tunnel-interface
       encapsulation ipsec preference 0
      !
      icmp-redirect-disable
      no shutdown
      shaping-rate          20480
      qos-map               COS-OUT-SHAPED
      bandwidth-upstream    20480
      bandwidth-downstream  20480
     !
     interface ge0/3
      description &quot;ORCH=NETWORK - CUSTOMER LAN - Service VPN physical interface&quot;
      mtu         1504
      no shutdown
    !
    PADB067# show run vpn 1
    vpn 1

Some routers don't have all blocks, in this case I want to find the block between "show run vpn 0" and the next routername, this is the regex.

(?&lt;=show run vpn 0)(?s)(.*?)(?=PADB067)

But I don't want it to start at "show run vpn 0 | i weight", that is another data block (that in this case is not present for that router).
So bottom line I'm searching for the correct regex that would 'think'

Find the text block after "show run vpn 0" when the line does not include "weight", until "PADB067"

Kr, Harry

答案1

得分: 0

With [tag:awk]:

awk '/PADB067# show run vpn 0/{p=1;next} /PADB067# show run vpn /{exit}p' file
英文:

With [tag:awk]:

awk &#39;/PADB067# show run vpn 0/{p=1;next} /PADB067# show run vpn /{exit}p&#39; file

答案2

得分: 0

你可以使用这个正则表达式:
(?&lt;=show run vpn 0 [\r\n])[\s\S]*?(?=\s*PADB067\b)
正则表达式解释:

  • (?&lt;=show run vpn 0 [\r\n]) -- 预期文本和换行符的正向先行断言
  • [\s\S]*? -- 非贪婪匹配([\s\S]*? 匹配任何字符,包括换行符;使用 .*?s 标志一起使用)
  • (?=\s*PADB067\b) -- 空白和具有单词边界的 PADB067 的正向先行断言

了解更多关于正则表达式的信息: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

英文:

You can use this regex:

(?&lt;=show run vpn 0 [\r\n])[\s\S]*?(?=\s*PADB067\b)

Explanation of regex:

  • (?&lt;=show run vpn 0 [\r\n]) -- positive lookbehind for expected text and newline
  • [\s\S]*? -- non-greedy scan ([\s\S]*? is any chars including newlines; use .*? instead with s flag)
  • (?=\s*PADB067\b) -- positive lookbehind for whitespace and PADB067 with word boundary

Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

答案3

得分: 0

你可以省略在线 (?s),然后匹配不以 PAD 和数字开头的一个或多个行,以获得更通用的方法,而不是硬编码的 PADB067

请注意,在示例数据中,在 vpn 0 后有一个空格。

解释

  • (?&lt;=show run vpn 0 ) 正向先行断言,确保文本直接位于当前位置的左侧。
  • (?: 非捕获组,作为整体重复
    • \n 匹配换行符(或使用 \r?\n
    • (?![^\S\n]*PADB\d+\b) 负向先行断言,确保字符串不以可选空格、后跟 PADB 和一个或多个数字的方式开头。
    • .* 匹配整行
  • )+ 关闭非捕获组,并重复 1 次或更多次。

正则表达式演示

英文:

You can omit the online (?s) and then match 1 or more lines that do not start with PAD and a digit if you want a more general approach instead of hardcoded PADB067.

Note that there is a space after vpn 0 in the example data

(?&lt;=show run vpn 0 )(?:\n(?![^\S\n]*PADB\d+\b).*)+

Explanation

  • (?&lt;=show run vpn 0 ) Positive lookbehind, assert that the text is directly to the left of the current position
  • (?: Non capture group to repeat as a whole part
    • \n Match a newline (Or use \r?\n)
    • (?![^\S\n]*PADB\d+\b) Negative lookahead, assert that the string does not start with optional spaces followed by PADB and 1 or more digits
    • .* Match the whole line
  • )+ Close the non capture group and repeat it 1 or more times

Regex demo

huangapple
  • 本文由 发表于 2023年2月24日 02:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定