Owlglass

PowerShell - Regex - string between two strings

Regex - string between two strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function GetStringBetweenTwoStrings($firstString, $secondString, $importPath){

    #Get content from file
    $file = gc -raw $importPath

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

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

    #Return result
    write-host $result

}