【发布时间】:2015-02-18 15:01:31
【问题描述】:
这很简单,但我无法理解它, 我正在尝试将 .ini 文件导入 powershell
我得到了这份清单
[IT] \\foo\bar1=123.123.123.123 \\foo\bar2 [Marketing] \\foo\bar3=123.123.123.123 \\foo\bar4=123.123.123.123
还有这段代码
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=(.*)" # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
我只是找不到正确的正则表达式来获取“\foo\bar2”
【问题讨论】:
-
那么在这三种情况中,
\\foo\bar2应该匹配什么?一个部分用方括号括起来,一个注释以分号开头,一个键包含一个等号...... -
它应该是一个key whiteout a value..所以我可以看看这个key是否被设置。
标签: regex powershell