【问题标题】:Regex to Match String in Multi-Line String正则表达式匹配多行字符串中的字符串
【发布时间】:2021-10-29 14:14:21
【问题描述】:

我在一个文本文件中有一些字符串。

line con 0
 session-timeout 10 
 exec-timeout 10 0
 privilege level 15
 logging synchronous
 login authentication console-in
 transport preferred none
 stopbits 1
line vty 0 4
 session-timeout 30 
 exec-timeout 30 0
 logging synchronous
 transport preferred none
 transport input ssh
 transport output ssh
line vty 5 15
 session-timeout 10 
 exec-timeout 10 0
 logging synchronous
 transport preferred none
 transport input ssh
 transport output ssh

我正在尝试查找/匹配

session-timeout 10
exec-timeout 10

仅在line con 0 下使用正则表达式。

line con 0.*\n(.*\n)*.*session-timeout 10.*\n(.*\n)*.*exec-timeout 10 也会捕获

vty 5 15
 session-timeout 10
 exec-timeout 10

有没有办法做得更好?

【问题讨论】:

  • 是整个文件吗?能不能只拿到第 2 行和第 3 行?
  • 这是文本格式的部分配置文件。我正在尝试使用正则表达式检测设置
  • this 你在找什么?
  • Thx @Alireza,如果行 (session timeout 10, exec-timeout 10) 在line con 0 之后但在line vty 0 之后没有显示,这仍然会产生误报

标签: .net regex


【解决方案1】:

如果 2 行 session-timeout 10exec-timeout 10 应按此顺序出现,则可以使用 2 个捕获组。

每个捕获组值,确保其前面的行不会交叉匹配 line ,例如 session- 用于第一个匹配, exec- 用于第二个匹配,使用负前瞻。

^line con 0(?:\r?\n(?!line | session-).*)*\r?\n[\p{Zs}\t]*(session-timeout 10).*(?:\r?\n(?!line| exec-).*)*\r?\n[\p{Zs}\t]*(exec-timeout 10)\b
  • ^ 字符串开始
  • line con 0 字面匹配
  • (?:\r?\n(?!line | session-).*)* 匹配所有不以line session- 开头的行
  • \r?\n[\p{Zs}\t]* 匹配换行符和可选的空格或制表符
  • (session-timeout 10) 捕获第 1 组,按字面匹配(或使用 [0-9]+ 匹配 1+ 位)
  • .* 匹配该行的其余部分
  • (?:\r?\n(?!line| exec-).*)* 匹配所有不以line exec- 开头的行
  • \r?\n[\p{Zs}\t]* 匹配换行符和可选的空格或制表符
  • (exec-timeout 10) 捕获第 2 组,逐字匹配
  • \b防止部分匹配的单词边界

查看regex 101 demo.NET regex demo(单击“表格”标签查看组)

【讨论】:

  • 谢谢@第四只鸟,这适用于捕获con 0 下方的 2 行。编写单独的检查以在line vty 0 4line vty 5 15 下捕获相同的两行的最佳方法是什么。我正在尝试识别不同块/标识符下的设置。
  • @NormanZhang 例如,您可以匹配数字而不是硬编码值,并在模式匹配行的开头后跟一个单词,然后是一个数字,然后是该行的其余部分 @987654352 @ 请参阅 regex101.com/r/obTjYF/1 对于完全匹配,您必须更新模式中的硬编码值。见regex101.com/r/eyIJ73/1
  • thx @第四只鸟,你的帮助很大。我在玩regex101.com/r/obTjYF/1,但无法执行以下模式的完全匹配。如果line vty 下的任何行没有session-timeout 10exec-timeout 10。寻找模式的值不是 10。
  • @NormanZhang 您可以使用^line vty [0-9].*((?:\r?\n(?!line vty [0-9]| (?:session|exec)-timeout 10).*)*)regex101.com/r/Zjs5Pd/1@regex101.com/r/Zjs5Pd/1捕获 vty 行下 session-timeout 和 exec-timeout 的值都不是 10 的部分
  • 谢谢@第四只鸟。在line vty 0 4regex101.com/r/Zjs5Pd/3之后没有捕捉到第一个exec-timeout 30有没有办法匹配?
【解决方案2】:

这是因为贪婪行为是正则表达式匹配的默认行为(它在匹配模式的同时尝试尽可能地扩展匹配)。在量词+* 之后添加? 可以解决贪心问题。

因此使用模式:

line con 0.*?\n(.*?\n)*?.*?session-timeout 10.*?\n(.*?\n)*?.*?exec-timeout 10

尝试匹配尽可能少的行。

匹配

line con 0
 session-timeout 10 
 exec-timeout 10

【讨论】:

  • line con 0.*?\n(.*?\n)*?.*?session-timeout 10.*?\n(.*?\n)*?.*?exec-timeout 10 catches false line (below vty 5) ``` line con 0 session-timeout 15 exec-timeout 15 0 权限级别 15 记录同步登录身份验证控制台输入传输首选无停止位 1 行vty 0 4 session-timeout 15 exec-timeout 15 0 记录同步传输首选无传输输入 ssh 传输输出 ssh 行 vty 5 15 会话超时 10 exec-timeout 10 0 记录同步传输首选无传输输入 ssh 传输输出 ssh ```
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 2012-06-05
  • 2013-12-25
相关资源
最近更新 更多