1

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  

Screenshot showing Matches even though I'm clearing

2
  • 2
    I get an empty $Matches output - are you sure you ran your code in a clean console? Commented Jul 20, 2021 at 13:07
  • 1
    Odd, I was typing clear in the the console then also added clear at the first line and was still getting matches. Closed the ISE down and opened again and now I don't. Commented Jul 20, 2021 at 13:20

1 Answer 1

1

You are mistaking what clear does. That cmdlet only clears the screen, not variables. If you want to clear out the contents of the automatic variable $Matches you will want to use Clear-Variable Matches instead. What you are seeing is expected behavior.

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

2 Comments

Fabulous, thank you. I was indeed mistaking what clear did!
If this answer solves the issue please use the checkmark to the left to mark it as accepted to help future users with similar issues find functional answers more quickly.

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.