I'm trying to get the path of an installed application from a string using Regex but can't understand the results I'm seeing.
My string includes the path I want to find "C:\GPONew\XPP\xz\bin" and out of that I want to capture the "C:\GPONew\XPP" and to test the script I'm testing for "\xzX\bin" to give a false result:
$path = "C:\GPONew\XPP\Perl\site\bin;C:\GPONew\XPP\Perl\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\GPONew\XPP\xz\bin"
$path -match "([A-Z]{1}:\\[^;]*\\XPP)\\xzX\\bin"
$Matches
And this outputs False for $Matches but does give me capture groups which I don't understand as it shouldn't match with the extra X in "xz".
False
Name Value
---- -----
1 C:\GPONew\XPP
0 C:\GPONew\XPP\xz\bin
On top of this, the docs Groups, Captures, and Substitutions go on to say that I can address $Matches using any hashtable method to access the value stored.
$Matches.0 or $Matches.1 but this gives me the error:
Unexpected token '.0' in expression or statement.
+ $Matches.0 <<<<
+ CategoryInfo : ParserError: (.0:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
And I have to instead use $Matches[0]
I am though using Powershell V 2.0 if that makes a difference?
EDIT:
After Wiktor Stribiżew's comment I closed my ISE down and opened it back up again and no longer get any matches with a False match but as soon as I change the match so it is True and then change it back so that it is false, I continue to get matches even if I have the following:
clear
$path = "C:\GPONew\XPP\Perl\site\bin;C:\GPONew\XPP\Perl\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\GPONew\XPP\xz\bin"
$path -match "([A-Z]{1}:\\[^;]*\\XPP)\\xzABC\\bin"
clear
$Matches
Output
False
Name Value
---- -----
1 C:\GPONew\XPP
0 C:\GPONew\XPP\xz\bin

$Matchesoutput - are you sure you ran your code in a clean console?