2016-11-09 135 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