0

I'm stuck with a regex issue. I have a text file that has lots of application names with different random characters in the beginning of it and the end of it. Some applications have parentheses to show the bit version of it (x64) and (x86). I go thru each application, take the name and pass it to my function. From there I compare it with another text file to see if it has "OK" as it's installed. However, my function uses a regex and when I pass my application name for comparison, it just fails since parentheses are regex characters. here is my function

function Get_End($Application)
{
    $EndRegex = [regex]"($Application).*(END OK)"
    try
    {
        $result = Get-Content "C:\temp\test.log" | Where-Object{ $_ -Match 
        $EndRegex }
        if($result -eq $null)
        {
           return "Not Found"
        }
        else
        {
            return $Matches[2]
        }
    }
    catch
    {
        Return "ERROR"
    }
} 

And this is how I pass my variable name:

Get_End($ApplicationName)

I tried putting $ in front uses {} instead of () in my function with no luck.

Anyone know how to get around this?

Thanks in advance!

Besiktas
  • 331
  • 1
  • 6
  • 23
  • Be careful calling your function that way. The correct syntax is just `Get_End $ApplicationName`. Your syntax will work, but remember that parentheses are a grouping operator, and while member functions use them like C# does, user defined functions will not! If you ever make a 2 argument function then you'll find that `MyFunction($Arg1, $Arg2)` will fail because PowerShell will try to pass both $Arg1 and $Arg2 to the first argument and not pass anything to the second argument. – Bacon Bits Aug 22 '17 at 12:50
  • Thanks for your response @BaconBits. You are completely correct. I've been switching between C# and powershell and you made me realize I was calling it like I do in C#. Thank you for that. – Besiktas Aug 22 '17 at 13:21

2 Answers2

2

If you are worried about regex control characters being passed to your function make your function assume the worst.

$EndRegex = "($([regex]::Escape($Application))).*(END OK)"

That will automatically add escape characters in front all control characters found in $application. $EndRegex is not being used as a [regex] object so that cast is redundant here. Keeping it as a string works just fine here.

Matt
  • 45,022
  • 8
  • 78
  • 119
1
$EndRegex = [regex]"($Application).*(END OK)"

You've got lots of control characters in this string. Presumably the .* is the only regex pattern you're actually trying to use? In that case you need to escape any control characters in $Application as well as the parentheses in your string. The escape character for regex strings is a backslash.

I would do this:

$EndRegex = [regex]("\({0}\).*\(END OK\)" -f [RegEx]::Escape($Application))
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • Thanks @BaconBits. This works but I think Matt's is a lot simpler. Thanks again for your quick response. – Besiktas Aug 22 '17 at 13:23