需要正则表达式来匹配多行,直到在公共分隔符之间找到匹配 [英] Need Regex to match multiple lines until Match is found between common delimiters

查看:42
本文介绍了需要正则表达式来匹配多行,直到在公共分隔符之间找到匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式,它将从日志文件中返回多行匹配项.使用下面的示例——我想匹配整个事务",它以与日志中所有其他事务(开始和结束)相同的文本开始和结束.但是 - 在这些行之间有一个自定义标识符 - 在这种情况下是一个电子邮件地址,可以将一项交易与另一笔交易区分开来.

I am attempting to write a regex that will return a multiple line match from a log file. Using the sample below -- I want to match an entire 'transaction' which begins and ends with the same text as ALL other transactions in the log (Start and End). However - between those lines there is a custom identifier -- in this case an email address that will differentiate one transaction from another.

Start of a transaction.
random line 1.
random line 2.
email1@gmail.com
End of a transaction.
Start of a transaction.
random line 1.
random line 2.
email1@yahoo.com
random line 3.
End of a transaction.

这是我的开始:

^Start(.*?)\n(((.*?)(email1\@gmail\.com)(.*?)|(.*?))\n){1,}End (.*?)\n

本质上 - 我想说:从开始"开始——并匹配所有行直到结束"行,但仅当其中一行包含特定电子邮件地址时才返回匹配项.

Essentially - I want to say: Begin with 'Start' -- and match all lines until an 'End' line, but only return a match if one of the lines contains a particular email address.

现在——我的正则表达式将整个日志文件视为单个匹配项,因为大概第 1 行包含一个开始",第 X 行包含一个结束",中间有数百行——它们是一个匹配项对于电子邮件.另外 -- 应用程序是 Powershell,如果重要的话,将使用 Select-String 模式.

Right now -- my regex treats the entire log file as a single match since presumably line 1 contains a 'Start' and line X contains an 'End' and somewhere in the hundreds of lines in between -- their is a match for the email. Also -- application is Powershell and will be using a Select-String pattern, if that matters.

推荐答案

使用 否定前瞻断言 以确保您的正则表达式永远不会跨越事务结束"边界匹配:

Use a negative lookahead assertion to make sure your regex never matches across an "End of transaction" boundary:

preg_match_all(
    '/^                                # Start of line
    Start\ of\ a\ transaction\.        # Match starting tag.
    (?:                                # Start capturing group.
     (?!End\ of\ a\ transaction)       # Only match if we\'re not at the end of a tag.
     .                                 # Match any character
    )*                                 # any number of times.
    email1@gmail\.com                  # Match the required email address
    (?:(?!End\ of\ a\ transaction).)*  # and the rest of the tag.
    ^                                  # Then match (at the start of a line)
    End\ of\ a\ transaction\.\n        # the closing tag./smx', 
    $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

测试 在 regex101.com 上直播.

这篇关于需要正则表达式来匹配多行,直到在公共分隔符之间找到匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆