如何在powershell上使用break语句" switch -regex -file"?

时间:2015-04-16 01:03:12

标签: regex powershell

我正在使用下一个函数来解析一个ini文件。

我有一个问题,我不知道如何解决:

如果找到一个匹配,则断开循环并读取下一行。

我知道我可以放手去做。但是,打破所有开关代码的退出。 ¿?

我认为使用带有正则表达式的switch来解析文件是一种特殊情况。

¿如何解析两个代码块中的同一行?

见输出:

`Comment ; Parameters that are strings are specified parameter=string, like this:`
`Key ; Parameters that are strings are specified parameter=string, like this:`

and

`Comment ; Parameters that are numbers are specified parameter=number, like this:` 
`Key ; Parameters that are numbers are specified parameter=number, like this:`

谢谢

    function Get-IniContent ($filePath)  
    {  
        $ini = @{}   
        switch -regex -file $FilePath   
        {   
            "^\[(.+)\]$" # Section   
            {   
                $section = $matches[1]   
                $ini[$section] = @{}   
                $CommentCount = 0   
                Write-Host "Section $section"  
            }   
            "^(;.*)$" # Comment   
            {   
                if (!($section))   
                {   
                    $section = "No-Section"   
                    $ini[$section] = @{}   
                }   
                $value = $matches[1]   
                $CommentCount = $CommentCount + 1   
                $name = "Comment" + $CommentCount   
                $ini[$section][$name] = $value   
                Write-Host "Comment $value"  
            }    
            "(.+?)\s*=\s*(.*)" # Key   
            {   
                if (!($section))   
                {   
                    $section = "No-Section"   
                    $ini[$section] = @{}   
                }   
                $name,$value = $matches[1..2]   
                $ini[$section][$name] = $value   
                Write-Host "Key $name=$value"  
            }   
            default  
            {              
                # Next line causes NullArray error  
                #$line=$matches[1]  
                Write-Host "Strange line $line"  
            }  
        }   

        return $ini  
    }  

    $iniFile=Get-IniContent (Join-Path (Split-Path -parent $MyInvocation.MyCommand.Path) "test.config")   

这是一个示例ini文件:

Independent Bad Line
timer=19
[Common]
; 4 - default value hard-coded in ntfs-hardlink-backup.ps1 script

; Blank lines also do no harm

; Parameters that are strings are specified parameter=string, like this:
backupDestination=X:\Backup
; Parameters that are numbers are specified parameter=number, like this:
backupsToKeep=20

[server-01.mycompany.example.org]
; Parameters that are specific to a particular server/computer go in a section for that computer.
; The section name is the fully-qualified domain name (FQDN) of the computer.
backupSources=D:\Shares\Admin,E:\Shares\Finance,E:\Shares\ICT,D:\Shares\Users
:Bad Line
=10 ; Also Bad Line

这是脚本输出:

E:\Config\NtfsBackup>powershell -ExecutionPolicy unrestricted -file "E:\Config\NtfsBackup\nt1.ps1" 
Strange line 
Key timer=19
Section Common
Comment ; 4 - default value hard-coded in ntfs-hardlink-backup.ps1 script
Strange line 
Comment ; Blank lines also do no harm
Strange line 
Comment ; Parameters that are strings are specified parameter=string, like this:
Key ; Parameters that are strings are specified parameter=string, like this:
Key backupDestination=X:\Backup
Comment ; Parameters that are numbers are specified parameter=number, like this:
Key ; Parameters that are numbers are specified parameter=number, like this:
Key backupsToKeep=20
Strange line 
Section server-01.mycompany.example.org
Comment ; Parameters that are specific to a particular server/computer go in a section for that computer.
Comment ; The section name is the fully-qualified domain name (FQDN) of the computer.
Key backupSources=D:\Shares\Admin,E:\Shares\Finance,E:\Shares\ICT,D:\Shares\Users
Strange line 
Strange line 

1 个答案:

答案 0 :(得分:3)

答案很简单。您不想使用Break,而是使用Continue,这将停止当前项的循环并启动下一个项的循环。将Continue放置在所有脚本块的末尾(可能在Write-Host行之后的新行上),可以满足您的需要。

function Get-IniContent ($filePath)  
{  
    $ini = @{}   
    switch -regex -file $FilePath   
    {   
        "^\[(.+)\]$" # Section   
        {   
            $section = $matches[1]   
            $ini[$section] = @{}   
            $CommentCount = 0   
            Write-Host "Section $section"
            Continue
        }   
        "^(;.*)$" # Comment   
        {   
            if (!($section))   
            {   
                $section = "No-Section"   
                $ini[$section] = @{}   
            }   
            $value = $matches[1]   
            $CommentCount = $CommentCount + 1   
            $name = "Comment" + $CommentCount   
            $ini[$section][$name] = $value   
            Write-Host "Comment $value"  
            Continue
        }    
        "(.+?)\s*=\s*(.*)" # Key   
        {   
            if (!($section))   
            {   
                $section = "No-Section"   
                $ini[$section] = @{}   
            }   
            $name,$value = $matches[1..2]   
            $ini[$section][$name] = $value   
            Write-Host "Key $name=$value"  
            Continue
        }   
        default  
        {              
            # Next line causes NullArray error  
            #$line=$matches[1]  
            Write-Host "Strange line $line"  
        }  
    }   

    return $ini  
}  

$iniFile=Get-IniContent (Join-Path (Split-Path -parent $MyInvocation.MyCommand.Path) "test.config")