Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Powershell Operator

Reply
 
Old 08-18-2008   #1 (permalink)
AdityaKir
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 SpecsSystem Spec
Old 08-18-2008   #2 (permalink)
Kiron
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 SpecsSystem Spec
Old 08-18-2008   #3 (permalink)
Kryten
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 SpecsSystem Spec
Old 08-18-2008   #4 (permalink)
Shay Levy [MVP]
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46