2016-11-09 134 views
-3

我有一個,可能是簡單的任務,即時嘗試解決 - 迄今沒有任何成功。 我想用powershell來解析和匹配一個字符串到變量中。 字符串的形式正則表達式和powershell

"Message 
------- 
RECORDING XYZ started recording on 2016-11-08 19:58:03 and stopped on 2016-11-08 20:33:00 as scheduled." 

的,我希望它產生三個變量$標題= 「錄製XYZ」,$ START_TIME = 「2016年11月8日19時58分03秒」,$ STOP_TIME =「 2016-11-08 20:33:00「。

是正則表達式的路還是powershell有更簡單的函數?我一直在看 - 分裂和 - 匹配前。

請問有沒有人有時間給我一隻手? 問候


編輯:

馬蒂亞斯的回答使我這個解決方案

$text = "Message 
------- 
RECORDING XYZ started recording on 2016-11-08 19:58:03 and stopped on 2016-11-08 20:33:00 as scheduled." 

$lines = $text -split "\n" 

$lines[2] -match "^(.+) started recording on (.+) and stopped on (.+) as scheduled." 

Write-Output $Matches[1] 

簡單,但它的工作原理

+0

東西=「消息 ------- 錄製XYZ開始記錄上2016-11 -08 19:58:03並在2016-11-08 20:33:00按計劃停止。「$ a = $ text -split」\ n「 $ b = $ a -match」^(。+)\按照計劃在\ s(。+)上開始錄製並停在(。+)上。「$ title = $ t [1] – Jiinxy

回答

0

可以使用named capture group抓住從串幾場比賽:

if($text -match "-`r?`n(?<recording>.*) started recording on (?<starttime>[\d\-\:\s]+) and stopped on (?<stoptime>[\d\-\:\s]+) as scheduled.") { 
    $title = $Matches['recording'] 
    $start_time = $Matches['starttime'] 
    $stop_time = $Matches['stoptime'] 
} 
+0

Cheers m8 - 對我來說沒有什麼作用,但它使我轉向一個可行的解決方案(請參閱我的編輯問題) – Jiinxy

0

我修改您的嘗試(您在評論中提及),沿$文本行分割

$text = "Message ------- RECORDING XYZ started recording on 2016-11-08 19:58:03 and stopped on 2016-11-08 20:33:00 as scheduled." 
$a = $text -split "\n" 
$arr = ((($a -split "Message ------- ")-split "started recording on") -split "and stopped on") -split "as scheduled." 

$title = $arr[1] 
$start_time = $arr[2] 
$stop_time = $arr[3] 

write-host $title $start_time $stop_time 
+0

乾杯,與我的工作解決方案非常相似。 – Jiinxy