Understanding Tradeoffs When Searching for a String

Doctor Scripto

Summary:  Microsoft Scripting Guy, Ed Wilson, provides an excerpt from his new book, Windows PowerShell Best Practices, about searching for strings.

Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new book, PowerShell Best Practices , which is published by Microsoft Press.

Image of book

When you are searching for a string, you can use at least three operators: −contains, −like, or −match.

The −contains operator

The most confusing of the bunch is the −contains operator. This is not due to its complexity of use, but rather, to an attempt at understanding when to use the operator. Perhaps a few examples will help.

In the following script, an array of numbers is created and stored in the $a variable. Next, the −contains operator is used to see whether the array that is stored in the $a variable contains the number 1. It does, and True is reported. The −contains operator is then used to see whether the $a array contains the number 6. It does not, and False is reported.

PS C:\> $a = 1,2,3,4,5
PS C:\> $a -contains 1
True
PS C:\> $a -contains 6
False

In the next example, the number 12345 is stored in the $b variable. The −contains method is used to see whether the number that is stored in $b contains the number 4. Although the number 4 is indeed present in the number 12345, −contains reports False. The number stored in $b does not contain 4. Next, the −contains method is used to see whether $b contains 12345, which it does, and True is reported.

PS C:\> $b = 12345
PS C:\> $b -contains 4
False
PS C:\> $b -contains 12345
True

Suppose that the variable $c stores the following string:

“This is a string.”

When the −contains method is used to look for the string “is”, False is returned. If the −contains method is used to look for the string “This is a string”, it returns True as shown here.

PS C:\> $c = "This is a string"
PS C:\> $c -contains "is"
False
PS C:\> $c -contains "This is a string"
True

For our last example of the −contains operator, if $d contains an array of strings ("This","is","a","string") when the −contains operator is used to look for the value of “is”, True is returned. When the −contains operator looks for “ring”, it returns False.

PS C:\> $d = "This","is","a","string"
PS C:\> $d -contains "is"
True
PS C:\> $d -contains "ring"
False

The −contains operator is used to examine the elements of an array. If the array contains a particular value, the operator returns True. If there is not an exact match for the value, the −contains operator returns False. This process can be an easy way to locate items in an array.

The −like operator

The −like operator is used to perform a wildcard search in a string. If the $a variable is used to hold the string “This is a string”, and the −like operator searches for “*ring*” , the −like operator returns True as shown here.

PS C:\> $a = "This is a string"
PS C:\> $a -like "*ring*"
True

An interesting use of the −like operator is to search the elements of an array. If the $b variable is used to hold the array “This”,”is”,”a”,”string” , and the −like operator searches the array for “*ring*”, every match for the wildcard pattern is returned—not just a True or False answer.

PS C:\> $b = "This","is","a","string"
PS C:\> $b -like "*ring*"
string

The −match operator

The −match operator is used to perform a regular expression pattern match. When the match is found, True is returned. If a match is not found, False is returned. If the $a variable is assigned the value "This is a string", and the −match operator is used to look for the value of “is”, the pattern is a match, and True is returned as shown here.

PS C:\> $a = "This is a string"
PS C:\> $a -match "is"
True

More complex match patterns can be used. For example, the \w character is used with regular expressions to look for any white space, such as a space before or after a letter. When the $a variable is used to hold the string "This is a string" and the regular expression pattern [\w a \w] is used, a match will be returned if the letter a is found with a space in front and a space behind the letter, as shown here.

PS C:\> $a = "This is a string"
PS C:\> $a -match "[\w a \w]"
True

What about matching with an array? If the $c variable is used to hold the array  "This","is","a","string" and the regular expression pattern match “is” is used, two matches are found. In this example, the actual string that contains the pattern match is returned. When a match is found, an array of strings is returned.

PS C:\> $c = "This","is","a","string"
PS C:\> $c -match "is"
This
is

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon