【发布时间】: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 "`${(.*?)}" } 但结果为空
-
你能把你文件里的内容贴出来吗?