![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| Guest | Powershell Operator hi, I need to do some string comparison but the below script doesnt seem to return true $version = "Microsoft(R) Windows(R) Server 2003, Enterprise Edition" $check = "Microsoft(R) Windows(R) Server 2003*" I am checking if the $Check exists in $Version $Version -match $check Returns False. Should it return true. Using -Like works dont know why ? |
My System Specs![]() |
| | #2 (permalink) |
| Guest | RE: Powershell Operator Complementing Keith's answer: # the -like operator does wildcard comparison # the -match operator does RegEx comparison # $check contains RegEx special characters that must be escaped to # include them in the pattern. Remove the '*' and call [RegEx]'s # Escape Method to create an escaped RegEx $version = "Microsoft(R) Windows(R) Server 2003, Enterprise Edition" $check = "Microsoft(R) Windows(R) Server 2003" $version -match [regex]::escape($check) # these are the RegEx special characters that must be escaped #.$^{[()*+?\| -- Kiron |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Powershell Operator Hi AdityaKir, Kiron's post pretty much says it all. One possible way to express $check in order to get a match with $version would be:- Kryten -->$version = "Microsoft(R) Windows(R) Server 2003, Enterprise Edition" Kryten -->$check = "Microsoft\(R\) Windows\(R\) Server 2003*" Kryten -->$version -match $check True Kryten -->$matches Name Value ---- ----- 0 Microsoft(R) Windows(R) Server 2003 Here $check has all the '(' and ')' s escaped with the '\'. From studying previous posts from those waaaay more knowledgable than I'll _ever_ be I learned that treating the -match value as a regex was generally a good idea and I've had a lot more success with it since. Good luck, Stuart |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Powershell Operator Hi AdityaKir, When you don't know what regex meta characters your variable might contain, there is a great method to *auto* escape them: PS > $check = "Microsoft(R) Windows(R) Server 2003*" PS > $escaped = [regex]::escape($check) Microsoft\(R\)\ Windows\(R\)\ Server\ 2003\* --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic A> hi, A> A> I need to do some string comparison but the below script doesnt seem A> to return true A> A> $version = "Microsoft(R) Windows(R) Server 2003, Enterprise Edition" A> $check = "Microsoft(R) Windows(R) Server 2003*" A> A> I am checking if the $Check exists in $Version A> A> $Version -match $check A> A> Returns False. Should it return true. Using -Like works dont know why A> ? A> |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| -ne operator | PowerShell | |||
| -f operator | PowerShell | |||
| What does the $() operator do? | PowerShell | |||
| Suggestion: 1 new operator (well, maybe 6...) | PowerShell | |||
| Ternary Operator in Powershell | PowerShell | |||