首先解释一下我是 php 新手和我对 preg_match 非常陌生并且觉得它很混乱,我试图做的是找到一个关键字:异常:然后从下一行开始拉出 300 个字符
我已经为此准备了一个预匹配但想改进它,我正在做的是从关键字中提取 300 个字符,但问题是关键字之后是异常名称,然后在下一行是代码错误,异常可以用任意数量的语言编写,但异常后的代码错误与语言无关,所以我想过滤掉异常,因为它因语言而异,所以我知道稍后比较时异常是否 100% 匹配。
以下是一些异常示例:
Exception: System.Runtime.InteropServices.COMException (0x800401D0): OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Windows.Clipboa
exception: Specified cast is not valid.
Query:Select * from TourneyData where Player_id = 1412
14:14:18.868 [SetCurrentPlayer:12 - DatabaseBase.HandleDatabaseConnectionException] 4: System.InvalidCastException: Specified cast is not valid.
at NpgsqlTypes.NpgsqlTimeStamp.op_I
Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.Application.ThreadContext.ExitCommon(Boolean disposing)
at System.Windows.Forms.Application.ExitInternal()
at System.Windows.Forms.Application.Exit(C
所以我打算如何解决代码错误是在关键字异常之后的下一行显示所有信息:
在最后一个示例中,我想要的输出是:
at System.Windows.Forms.Application.ThreadContext.ExitCommon(Boolean disposing)
at System.Windows.Forms.Application.ExitInternal()
at System.Windows.Forms.Application.Exit(C
好的,这是我已经用来在关键字后面收集 300 个字符的代码:
// Snippet length constant
define(SNIPPET_LENGTH, 300);
$pos = stripos($body,$keyword);
$snippet_pre = substr($body, $pos, SNIPPET_LENGTH);
现在我还在一些函数中使用 preg_match 来提取信息,例如代码有这个查找日志信息:
12:19:42.787 [Main:1 - Bootstrapper.LogSystemInfo] Current culture: it-IT
12:19:42.865 [Main:1 - Bootstrapper.LogSystemInfo] Operating System Name: Microsoft Windows 7 Home Premium
12:19:42.865 [Main:1 - Bootstrapper.LogSystemInfo] Operating System Architecture: 64 bit
12:19:42.865 [Main:1 - Bootstrapper.LogSystemInfo] Operating System Service Pack: Service Pack 1
这是 preg_match,仅包括因为它可能有助于区分如何区分换行符,因为这会捕获换行符之前的所有信息,但我无法弄清楚如何在换行符之后获取 300 个字符:
preg_match('/Current culture: (.*)/', $body, $culture_pre);
preg_match('/Operating System Name: (.*)/', $body, $os_name_pre);
preg_match('/Operating System Architecture: (.*)/', $body, $os_bit_pre);
preg_match('/Operating System Service Pack: (.*)/', $body, $os_service_pack_pre);
如果您需要任何其他信息,请告诉我