2017-06-30 86 views
1

我正在嘗試收集日誌文件中2個字符之間的所有文本。在PowerShell中獲取2個字符之間的所有文本

的例子日誌文件文本是:

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

我想收集所有的第一架和最後一個波浪之間的文本。

我一直在嘗試使用REGEX來做到這一點,但我不確定正確的模式。

誰能幫助?

+1

允許的點以匹配新行:' '\(*〜+(S?)?'' – wOxxOm

+0

我得到了我提供錯誤的字符串。有人可以幫我用這個字符串和一個正則表達式嗎? (9:39:40 AM)directorbot:我們有一個忙碌的週末。 ~~~這是從Boris到All的一個廣播在2017-09-28 08:39:40.858803 EVE ~~~ 從第一個(到最後一個~~~ – kendarr

回答

1

每從wOxxOm的評論,此正則表達式模式似乎這樣的伎倆:'(?s)\(.*?~+'

這裏是你如何可以使用PowerShell使用它:

$Sample = @" 
Conversation with Boris at 6/30/2017 7:26:35 AM on Server(jabber) 
(7:26:41 AM) [email protected]/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) [email protected]/Server: I likes cake 
This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~ 
1

如果您的要求如此固定以至於第一個'('和最後一個'〜',我會給你一個更簡單的解決方案,而不使用Regex。看看它吧

$x='Conversation with Boris at 6/30/2017 7:26:35 AM on Server(jabber) 
(7:26:41 AM) [email protected]/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) [email protected]/Server: I likes cake 
    This was a Message from Boris to everyone at 2017-06-29 22:35:18.724681 EVE ~~~ 

希望這有助於

相關問題