gpt4 book ai didi

regex - Powershell 正则表达式与 "other"正则表达式,有何不同?

转载 作者:行者123 更新时间:2023-12-01 13:32:51 36 4
gpt4 key购买 nike

我有一个 powershell 脚本来匹配以下正则表达式:

---\n(0[1-9]|1[0-2][\/](0[1-9]|[12]\d|3[01])[\/]\d{2}[\s\S]+?)-----

要匹配的字符串是以下日志文​​件片段:

------------------------------------------------------------------------------- 10/26/16 11:41:26 - Process(15925376.4) User(mqm) Program(amqzmuc0)                    Host(aixmq1) Installation(Installation1)                    VRMF(8.0.0.4) QMgr(ecs.queue.manager)                    AMQ6287: WebSphere MQ V8.0.0.4 (p800-004-151017).EXPLANATION: WebSphere MQ system information:  Host Info         :-AIX 7.1 (MQ AIX 64-bit)  Installation      :- /usr/mqm (Installation1)Version           :- 8.0.0.4 (p800-004-151017) ACTION: None.------------------------------------------------------------------------------- 10/26/16 11:41:26 - Process(15925376.4) User(mqm) Program(amqzmuc0)                    Host(aixmq1) Installation(Installation1)                    VRMF(8.0.0.4) QMgr(ecs.queue.manager)                    AMQ6287: WebSphere MQ V8.0.0.4 (p800-004-151017).EXPLANATION:FFF WebSphere MQ system information:  Host Info         :-AIX 7.1 (MQ AIX 64-bit)  Installation      :- /usr/mqm (Installation1)Version           :- 8.0.0.4 (p800-004-151017) ACTION: None.-------------------------------------------------------------------------------

Using this regex in perl and on regexr.com, it perfectly matches two sections from that logfile snippet.

Now i've implemented that same regex in powershell and it won't return any matches unless i remove the minuses preceding the \n. If i replace those minuses by a matching group that only contains minus, it will work aswell.

For the sake of consistency and understanding what is going on , i need to understand why the matching behavior is so different in powershell. Why won't it match as soon as there are minuses at the beginning?

The following .NET regex tester shows the same behavior as in powershell:

http://regexstorm.net/tester

Could someone please explain to me why the matching behavior is that different in powershell compared to perl/regexr.com?

This is the snippet of powershell code i'm currently using to match that regex:

$matches = ([regex]::matches($sInput, "---\n(0[1-9]|1[0-2][\/](0[1-9]|[12]\d|3[01])[\/]\d{2}[\s\S]+?)\n-") | %{$_.value});

最佳答案

在 Windows 上,行结尾(通常)是 CRLF(两个字符,回车然后换行),而在基于 unix 的操作系统(基本上除了 Windows 之外)它只是一个换行符 低频。转义序列 \n 指的是 LF。要匹配 CR,请使用 \r

所以我认为正在发生的事情是,如果您的输入包含 CRLF,那么 -\n 将不会匹配它。但是 \n 会因为它跳过前面的 CR

您用来测试它的网站可能会转换行结尾,或者没有正确保留它们,因此匹配,而 .Net 测试人员可能会做相反的事情。

作为引用,每当我需要匹配正则表达式中的行结尾时,我都会使用 \r?\n(可选的 CR 后跟 LF),这样我就可以捕获这两种类型的行尾。

因此在您的示例中,您应该能够将正则表达式的开头从 ---\n 更改为 ---\r?\n 并且如果我对您的具体问题是正确的,它会起作用。

关于regex - Powershell 正则表达式与 "other"正则表达式,有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928254/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com