Powershell正则表达式获取字符串和字符之间的字符串
Powershell regex to get string between string and char
我有这样的文件,里面有一些变量:
${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;"/>
有人可以给我提示吗?
谢谢
我会这样做:
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
中给出的字符串,您在调用该函数时不必为此操心。
此外,由于输入文件中可能有更多匹配项,该函数现在 returns 一个匹配项数组。
即如果您的输入文件包含这样的内容:
<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
我已经提供了@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
我有这样的文件,里面有一些变量:
${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;"/>
有人可以给我提示吗?
谢谢
我会这样做:
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
中给出的字符串,您在调用该函数时不必为此操心。
此外,由于输入文件中可能有更多匹配项,该函数现在 returns 一个匹配项数组。
即如果您的输入文件包含这样的内容:
<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
我已经提供了@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