Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Powershell使用正则表达式创建新的哈希表对象_Regex_Powershell - Fatal编程技术网

Regex Powershell使用正则表达式创建新的哈希表对象

Regex Powershell使用正则表达式创建新的哈希表对象,regex,powershell,Regex,Powershell,我正在尝试解析一个包含此格式文本块的文件(其中可能有1-n个): 如果有人感兴趣,文本来自MS Hotfix请求电子邮件 我有以下powershell one衬里: $file | select-string "(?<Name>\w+):(?<Value>.*)" -allmatches | SELECT @{N="Name";E={$_.Matches[0].Groups[1].Value}}, @{N="Value";E={$_.Matches[0].Groups[

我正在尝试解析一个包含此格式文本块的文件(其中可能有1-n个):

如果有人感兴趣,文本来自MS Hotfix请求电子邮件

我有以下powershell one衬里:

 $file | select-string "(?<Name>\w+):(?<Value>.*)" -allmatches | SELECT @{N="Name";E={$_.Matches[0].Groups[1].Value}}, @{N="Value";E={$_.Matches[0].Groups[2].Value}}
$file | select string“(?\w+:(?*)”-allmatches | select@{N=“Name”E={$\.Matches[0]。组[1]。值},@{N=“Value”;E={$\.Matches[0]。组[2]。值}
这给了我一组简单的名称-值对,我希望它返回一个哈希表数组,每个哈希表都填充了5个键


我不确定问题是否出在我的正则表达式上,也不确定我是如何访问匹配项的(必须有一种不太详细的方法来挑选这些匹配项)。

这不太好看,但我认为它对你有用:

$ht = @{}
$file | %{if ($_ -match '(?<Name>[^:]+):(?<Value>.*)') {
          $ht.($matches.Name) = $matches.Value.Trim()} `
          elseif ($ht.Count) {$ht;$ht.Clear()}}
$ht=@{}
$file |%{if($|-match'(?[^::+):(?.*)){
$ht。($matches.Name)=$matches.Value.Trim()}`
elseif($ht.Count){$ht;$ht.Clear()}

使用
-match
操作符稍微容易一些,因为您可以直接使用$matches变量,而不必通过Select字符串的MatchInfo.matches属性。顺便说一句,我假设$file是通过调用
Get Content
填充的,即每一行都是字符串数组中的一个字符串。

很抱歉耽搁了Keith,谢谢你的回答。我在另一个鼠洞里呆了一段时间(8)
$ht = @{}
$file | %{if ($_ -match '(?<Name>[^:]+):(?<Value>.*)') {
          $ht.($matches.Name) = $matches.Value.Trim()} `
          elseif ($ht.Count) {$ht;$ht.Clear()}}