在Powershell中获取2个字符之间的所有文本

时间:2017-06-30 06:54:41

标签: regex powershell

我正在尝试收集日志文件中2个字符之间的所有文本。

示例日志文件文本为:

Conversation with Boris at 6/30/2017 7:26:35 AM on Server(jabber)
(7:26:41 AM) Boris@Boris.com/Server:  I likes cake
This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~

我想收集第一个支架和最后一个支架之间的所有文字。

我一直在尝试使用REGEX来做这件事,但我不确定正确的模式。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

根据wOxxOm的评论,这个正则表达式似乎可以解决问题:'(?s)\(.*?~+'

以下是如何在PowerShell中使用它的方法:

$Sample = @"
Conversation with Boris at 6/30/2017 7:26:35 AM on Server(jabber)
(7:26:41 AM) Boris@Boris.com/Server:  I likes cake
This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~
"@

$Sample -Match '(?s)\(.*?~+' | Out-Null

$Result = $Matches.Values

此处$Result

(jabber)
(7:26:41 AM) Boris@Boris.com/Server:  I likes cake
This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~

答案 1 :(得分:1)

如果您的要求是如此固定,以至于第一次&#39;(&#39;以及最后&#39;〜&#39;,我会在不使用Regex的情况下为您提供更简单的解决方案。请查看它< / p>

$x='Conversation with Boris at 6/30/2017 7:26:35 AM on Server(jabber)
(7:26:41 AM) Boris@Boris.com/Server:  I likes cake
This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~'

$x.Substring($x.IndexOf('('),$x.IndexOf('~~~')-$x.IndexOf('(')+3)


 #Output
    (jabber)
    (7:26:41 AM) Boris@Boris.com/Server:  I likes cake
    This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~

希望这有帮助