0

I have file with some variables in it like this:

${variable}

And i want to loop through file and output:

variable
variable1
variable2
variable3

etc.

My code:

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"

EXAMPLE of input file line:

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

Can anybody give me a hint?

Thanks

8
  • I'm trying to determine the usefulness of this function. Normally, matching a pattern already takes place between strings (a string is just an array of chars). Commented Oct 26, 2018 at 7:29
  • yes, but this is not the point of the problem at all. I am just not able to take "variable" from that 'pattern' Commented Oct 26, 2018 at 7:33
  • you mean Get-Content $importPath | ? { string object -match "\\${(.*?)}" } Commented Oct 26, 2018 at 7:38
  • I've tried this Get-Content ".\vocGB_start.template" | ? {$_ -match "`${(.*?)}" } but with empty result Commented Oct 26, 2018 at 7:47
  • Can you post what is in your file? Commented Oct 26, 2018 at 7:50

2 Answers 2

3

I would do it like this:

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
}

and call te function:

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

Because the function now takes care of escaping the strings give in $firstString and $secondString, you don't have to bother about this when calling the function. Also, because there may be more matches in the input file, the function now returns an array of matches.

i.e. if your input file contains stuff like this:

<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;"/>

the returned matches will be

input.cardType
input.otherType
Sign up to request clarification or add additional context in comments.

1 Comment

@MartinFric You're welcome! By the way, It is better to rename the function to Get-StringBetweenTwoStrings so it complies with the Powershell naming convention for functions and cmdlets. You can read about that here or there. Especially when you are making a module and do not want PS to complain..
1

I've provided alternative implementation to the one @Theo proposed:

Script:

$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 file:

<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};"/>

Output:

input.cardType
input.second
input.third

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.