无法仅捕获两个引号之一 [英] Unable to capture just one of two quotes

查看:82
本文介绍了无法仅捕获两个引号之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在处理

So I'm working off of this blog post in an attempt to parse ini files. It works for the most part, but I'm running into a specific problem I don't know enough about regex to solve.

function Get-IniContent
{
    [CmdletBinding()]
    [OutputType([hashtable])]
    param
    (
        [Parameter(Position = 0, Mandatory, ValueFromPipelineByPropertyName)]
        [ValidateScript({Test-Path -Path $PSItem -PathType Leaf})]
        [Alias('FullName')]
        [string]
        $Path
    )

    process
    {
        $ini = @{}

        switch -Regex -File $Path
        {
            '^\[(?<Section>.+)\]$'
            {
                $section = $Matches['Section']
                $ini[$section] = @{}
                $commentCount = 0
            }

            '^;(?<Comment>.*)'
            {
                if (-not $section)
                {
                    $section = 'NoSection'
                    $ini[$section] = @{}
                }
                $commentCount += 1
                $ini[$section]["Comment$commentCount"] = $Matches['Comment']
            }

            '(?<Key>.+?)\s*=\s*(?<Value>.*)'
            {
                if (-not $section)
                {
                    $section = 'NoSection'
                    $ini[$section] = @{}
                }

                $ini[$section][$Matches['Key']] = $Matches['Value'] -replace
                    '^"(.*)"$', '$1' -replace
                    '\s*(.*)\s*', '$1'
            }
        }

        $ini
    }
}


问题:

在本节中:


The problem:

In this section:

$ini[$section][$Matches['Key']] = $Matches['Value'] -replace
     '^"(.*)"$','$1' -replace
     '\s*(.*)\s*','$1'

我遇到的情况是我的ini文件可能具有带引号的值,然后带有带引号的字符串:

I'm running into cases where my ini files may have quoted values that then have strings with quotes:

Key="  this value="something here""

我想要一个正则表达式字符串(最好是在开关捕获中),以避免那些双引号引起来.

I want a single regex string (ideally in the switch capture) to avoid those surrounding double-quotes.

我尝试在值的两边使用可选字符"?,但是它只能跳过起始引号,而不能跳过结束引号.

I tried using the optional character "? around both sides of the value, but it only managed to skip the starting quote, but not the ending quote.

示例字符串:

KeyName = "value:"ac-dii-sk""

尝试的模式:

$HashPattern = '\s*(?<Key>.+)\s*=\s*"?\s*(?<Value>.*)\s*"?\s*'

结果:

$Matches['Key']   = KeyName
$Matches['Value'] = value:"ac-dii-sk""

所需结果:

$Matches['Key']   = KeyName
$Matches['Value'] = value:"ac-dii-sk"

推荐答案

让我们尝试一下平衡捕获组.

(?<Key>.+?)\s*=\s*(?<open>")?(?<Value>.*?)(?<close-open>")?$


捕获的值:value:"ac-dii-sk"

Test it Online

Input: KeyName = "value:"ac-dii-sk""
Captured Value: value:"ac-dii-sk"

输入:KeyName = "value:"ac-dii-sk"(少了1个引号)
值:value:"ac-dii-sk

Input: KeyName = "value:"ac-dii-sk" (1 fewer end quote)
Value: value:"ac-dii-sk

输入:KeyName = value:"ac-dii-sk""(缺少引号)
值:value:"ac-dii-sk""

Input: KeyName = value:"ac-dii-sk"" (missing beginning quote)
Value: value:"ac-dii-sk""

输入:KeyName = value:"ac-dii-sk"(不带引号)
值:value:"ac-dii-sk"

Input: KeyName = value:"ac-dii-sk" (no surrounding quotes)
Value: value:"ac-dii-sk"

正如我在评论中提到的,我建议您仅使用现有的库来解析INI文件.这是PSGallery中的2个:

As I mentioned in a comment, I recommend you just use an existing library to parse INI files. Here are 2 from PSGallery:

  • Carbon
  • PsIni

这篇关于无法仅捕获两个引号之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆