【问题标题】:Powershell regex to get string between string and charPowershell正则表达式在字符串和字符之间获取字符串
【发布时间】:2019-03-30 21:39:27
【问题描述】:

我有一个包含一些变量的文件,如下所示:

${variable}

我想遍历文件并输出:

variable
variable1
variable2
variable3

等等

我的代码:

function GetStringBetweenTwoStrings($firstString, $secondString, $importPath){

    #Get content from file
    $file = Get-Content $importPath

    #Regex pattern to compare two strings
    $pattern = "$firstString(.*?)$secondString"

    #Perform the opperation
    $result = [regex]::Match($file,$pattern).Groups[1].Value

    #Return result
    return $result

}

GetStringBetweenTwoStrings -firstString "\\${" -secondString "}" -importPath ".\start.template"

输入文件行示例:

<input id="paymentMethod_VISA" type="radio" name="${input.cardType}" value="VISA" checked="checked" style="width: 1.5em; height: 1.5em;"/>

谁能给我一个提示?

谢谢

【问题讨论】:

  • 我正在尝试确定此功能的实用性。通常,匹配模式已经发生在字符串之间(字符串只是一个字符数组)。
  • 是的,但这根本不是问题的关键。我只是无法从那个“模式”中获取“变量”
  • 你的意思是 Get-Content $importPath | ? { 字符串对象 -match "\\${(.*?)}" }
  • 我试过这个 Get-Content ".\vocGB_start.template" | ? {$_ -match "`${(.*?)}" } 但结果为空
  • 你能把你文件里的内容贴出来吗?

标签: powershell regex-greedy


【解决方案1】:

我会这样做:

function GetStringBetweenTwoStrings($firstString, $secondString, $importPath){
    #Get content from file
    $file = Get-Content $importPath -Raw

    #Regex pattern to compare two strings
    $regex = [regex] $('{0}(.*?){1}' -f [Regex]::Escape($firstString), [Regex]::Escape($secondString))

    $result = @()
    #Perform and return the result
    $match = $regex.Match($file)
    while ($match.Success) {
        $result += $match.Groups[1].Value
        $match = $match.NextMatch()
    }
    return $result
}

并调用函数:

GetStringBetweenTwoStrings -firstString '${' -secondString '}' -importPath '<PATH_TO_YOUR_INPUT_FILE>'

因为该函数现在负责转义$firstString$secondString 中给出的字符串,所以在调用该函数时您不必为此烦恼。 此外,由于输入文件中可能有更多匹配项,该函数现在返回一个匹配项数组。

即如果您的输入文件包含以下内容:

<input id="paymentMethod_VISA" type="radio" name="${input.cardType}" value="VISA" checked="checked" style="width: 1.5em; height: 1.5em;"/>
<input id="paymentMethod_OTHER" type="radio" name="${input.otherType}" value="Other" checked="checked" style="width: 1.5em; height: 1.5em;"/>

返回的匹配将是

input.cardType
input.otherType

【讨论】:

  • @MartinFric 不客气!顺便说一句,最好将函数重命名为 Get-StringBetweenTwoStrings,这样它就符合函数和 cmdlet 的 Powershell 命名约定。您可以阅读有关 herethere 的信息。尤其是当你正在制作一个模块并且不想让 PS 抱怨时..
【解决方案2】:

我提供了@Theo 提议的替代实现:

脚本:

$path = ".\file.txt"
$content = Get-Content -Path $path -Raw
$m = $content | Select-String -pattern '\${(?<variable>[^}]+)}' -AllMatches
$m.matches.groups | Where-Object {$_.Name -eq "variable"} | ForEach-Object {Write-Output $_.Value}

输入文件:

<input id="paymentMethod_VISA" type="radio" name="${input.cardType}" value="VISA" checked="checked" style="width: 1.5em; height: 1.5em;"/> <input id="${input.second}" type="${input.third};"/>

输出:

input.cardType
input.second
input.third

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2022-11-17
    • 2020-03-23
    相关资源
    最近更新 更多