gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:27:42 28 4
gpt4 key购买 nike

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

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

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

我知道我可以休息一下。但是,中断所有开关代码的退出。 ¿?

我想这是使用带有正则表达式的开关来解析文件时的特殊情况。

¿我怎样才能不解析两个代码块中的同一行?

见输出:

`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")   

This it's a sample ini file:

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

最佳答案

答案很简单。您不想使用 Break而是使用 Continue这将停止当前项目的循环并开始下一个项目的循环。配售Continue在所有脚本 block 的末尾(可能在 Write-Host 行之后的新行上),switch 将满足您的需求。

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")

关于regex - 如何在 powershell "switch -regex -file"上使用 break 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29663589/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com