正则表达式出现几次,需要最后两行匹配

正则表达式出现几次,需要最后两行匹配

我正在尝试创建一个正则表达式来仅输出与请求匹配的最后 2 行。我有一个日志文件,上面只有几行,我想用以下命令捕获最后 2 行:

第一行:“4bccf”和“Media”

第二行:“Live”和“4bccf”和“my”

我为此创建了一个正则表达式: ((?=.*\b4bccf\b)(?=.*\bMedia\b)|(?=.*\bLive\b)(?=.*\b4bccf\b)(?=.*\bmy\b))

有效,但它会打印所有带有(“4bccf”和“Media”)的行 + 所有带有(“Live”和“4bccf”和“my”)的行

我只想获取每行的最后一行。是否可以只用一个正则表达式来实现这一点?

如果你有想法,我很感兴趣,谢谢

答案1

这是一个复杂的正则表达式(PCRE 风格)可以完成这项工作,但我建议您用您喜欢的脚本语言编写脚本。它将更易于维护!

笔记:我使用了/x允许注释且不考虑空格的标志,这对于可读性非常有用 ;-)。如果您的正则表达式不支持它,请删除所有空格和换行符。

^                                   # beginning of line
  (?=.*\b4bccf\b)                   # lookahead, make sure we have 4bccf in the line
  (?=.*\bMedia\b)                   # lookahead, make sure we have Media in the line
  (.+)                              # group 1, the whole line
  $                                 # end of line
  (?=                               # lookahead, make sure we have after:
    (?:                                 # non capture group
      (?!                               # negative lookahead, make we haven't after:
        (?=.*\b4bccf\b)                     # lookahead, make sure we have 4bccf in the line
        (?=.*\bMedia\b)                     # lookahead, make sure we have Media in the line
      )                                 # end lookahead
      [\s\S]                            # any character, including linebreak
    )*                                  # end group, may appear 0 or more times
    \z                                  # end of file
  )                                 # end lookahead
|                                 # OR
^                                   # same description as above for the other words to check
  (?=.*\bLive\b)
  (?=.*\b4bccf\b)
  (?=.*\bmy\b)
  (.+)
  $
  (?=
    (?:
      (?!
        (?=.*\bLive\b)
        (?=.*\b4bccf\b)
        (?=.*\bmy\b)
      )
      [\s\S]
    )*
    \z
  )

演示与说明

相关内容