【问题标题】:importing empty setting from .ini file从 .ini 文件导入空设置
【发布时间】: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


【解决方案1】:

您会认为您可以将= 设为可选,然后一切都会变得很花哨。

"(.+?)\s*=?(.*)"

但是,当您尝试使 = 成为可选的正则表达式 "(.+?)" 时,您将吞噬整个字符串,从而更难使用您拥有的 $matches 逻辑拆分内容。您应该更改开关,使其仅匹配每一行一次。然后您可以假设默认操作将在“键”上执行,因为所有其他行都已经匹配。注意下面的continues

function Get-IniContent ($filePath)
{
 $ini = @{}
 switch -regex -file $FilePath
 {
    "^\[(.+)\]" # Section
    {
        $section = $matches[1]
        $ini[$section] = @{}
        $CommentCount = 0
        continue
    }
    "^(;.*)$" # Comment
    {
        $value = $matches[1]
        $CommentCount = $CommentCount + 1
        $name = "Comment" + $CommentCount
        $ini[$section][$name] = $value
        continue
    } 
    default # Key
    {
        $name,$value = $_.split("=")
        $ini[$section][$name] = $value
    }
 }
 return $ini
}

样本输出

Name                           Value                                                                                                                         
----                           -----                                                                                                                         
IT                             {\\foo\bar1, \\foo\bar2}                                                                                                      
Marketing                      {\\foo\bar3, \\foo\bar4}  


(Get-IniContent c:\temp\file.ini).IT

Name                           Value                                                                                                                         
----                           -----                                                                                                                         
\\foo\bar1                     123.123.123.123                                                                                                               
\\foo\bar2                   

【讨论】:

  • 我不知道为什么我没有想到这个...感谢您的快速回复
【解决方案2】:

这将匹配所有\\foo\bar

\\\\foo\\bar\d?

demo.

如果你想匹配\\foo\bar2,使用

\\\\foo\\bar2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2010-09-10
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多