{"id":2171,"date":"2014-01-21T00:01:00","date_gmt":"2014-01-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/21\/understanding-tradeoffs-when-searching-for-a-string\/"},"modified":"2014-01-21T00:01:00","modified_gmt":"2014-01-21T00:01:00","slug":"understanding-tradeoffs-when-searching-for-a-string","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/understanding-tradeoffs-when-searching-for-a-string\/","title":{"rendered":"Understanding Tradeoffs When Searching for a String"},"content":{"rendered":"<p><b>Summary<\/b>: &nbsp;Microsoft Scripting Guy, Ed Wilson, provides an excerpt from his new book, Windows PowerShell Best Practices, about searching for strings.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-Best-Practices-Wilson\/dp\/0735666490\/ref=sr_1_2?ie=UTF8&amp;qid=1386004821&amp;sr=8-2&amp;keywords=powershell+best+practices\" target=\"_blank\">PowerShell Best Practices<\/a> , which is published by Microsoft Press.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/bestpracticebookcover.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/bestpracticebookcover.jpg\" alt=\"Image of book\" title=\"Image of book\" \/><\/a><\/p>\n<p>When you are searching for a string, you can use at least three operators:&nbsp;<b>&minus;contains<\/b>, <b>&minus;like<\/b>, or <b>&minus;match<\/b>.<\/p>\n<h2>The &minus;contains operator<\/h2>\n<p class=\"SbarParafirst\">The most confusing of the bunch is the <b>&minus;contains<\/b> 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.<\/p>\n<p class=\"SbarParafirst\">In the following script, an array of numbers is created and stored in the <b>$a<\/b> variable. Next, the <b>&minus;contains<\/b> operator is used to see whether the array that is stored in the <b>$a<\/b> variable contains the number 1. It does, and <b>True<\/b> is reported. The <b>&minus;contains<\/b> operator is then used to see whether the <b>$a<\/b> array contains the number 6. It does not, and <b>False<\/b> is reported.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $a = 1,2,3,4,5<br \/> PS C:\\&gt; $a -contains 1<br \/> True<br \/> PS C:\\&gt; $a -contains 6<br \/> False<\/p>\n<p class=\"SbarParamid\">In the next example, the number 12345 is stored in the <b>$b<\/b> variable. The <b>&minus;contains<\/b> method is used to see whether the number that is stored in <b>$b<\/b> contains the number 4. Although the number 4 is indeed present in the number 12345, <b>&minus;contains<\/b> reports <b>False<\/b>. The number stored in <b>$b<\/b> does not contain 4. Next, the <b>&minus;contains<\/b> method is used to see whether <b>$b<\/b> contains 12345, which it does, and <b>True<\/b> is reported.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $b = 12345<br \/> PS C:\\&gt; $b -contains 4<br \/> False<br \/> PS C:\\&gt; $b -contains 12345<br \/> True<\/p>\n<p class=\"SbarParamid\">Suppose that the variable <b>$c<\/b> stores the following string:<\/p>\n<p class=\"SbarParamid\" style=\"margin-left:30px\">&ldquo;This is a string.&rdquo;<\/p>\n<p class=\"SbarParamid\">When the <b>&minus;contains<\/b> method is used to look for the string <b>&ldquo;is&rdquo;<\/b>, <b>False<\/b> is returned. If the <b>&minus;contains<\/b> method is used to look for the string <b>&ldquo;This is a string&rdquo;<\/b>, it returns <b>True<\/b> as shown here.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $c = &quot;This is a string&quot;<br \/> PS C:\\&gt; $c -contains &quot;is&quot;<br \/> False<br \/> PS C:\\&gt; $c -contains &quot;This is a string&quot;<br \/> True<\/p>\n<p class=\"SbarParamid\">For our last example of the <b>&minus;contains<\/b> operator, if <b>$d<\/b> contains an array of strings (<b>&quot;This&quot;,&quot;is&quot;,&quot;a&quot;,&quot;string&quot;<\/b>) when the <b>&minus;contains<\/b> operator is used to look for the value of <b>&ldquo;is&rdquo;<\/b>, <b>True<\/b> is returned. When the <b>&minus;contains<\/b> operator looks for <b>&ldquo;ring&rdquo;<\/b>, it returns <b>False<\/b>.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $d = &quot;This&quot;,&quot;is&quot;,&quot;a&quot;,&quot;string&quot;<br \/> PS C:\\&gt; $d -contains &quot;is&quot;<br \/> True<br \/> PS C:\\&gt; $d -contains &quot;ring&quot;<br \/> False<\/p>\n<p class=\"SbarParamid\">The <b>&minus;contains<\/b> operator is used to examine the elements of an array. If the array contains a particular value, the operator returns <b>True<\/b>. If there is not an exact match for the value, the <b>&minus;contains<\/b> operator returns <b>False<\/b>. This process can be an easy way to locate items in an array.<\/p>\n<h2>The &minus;like operator<\/h2>\n<p class=\"SbarParamid\">The <b>&minus;like<\/b> operator is used to perform a wildcard search in a string. If the <b>$a<\/b> variable is used to hold the string &ldquo;This is a string&rdquo;, and the <b>&minus;like<\/b> operator searches for <b>&ldquo;*ring*&rdquo;<\/b> , the <b>&minus;like<\/b> operator returns <b>True<\/b> as shown here.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;This is a string&quot;<br \/> PS C:\\&gt; $a -like &quot;*ring*&quot;<br \/> True<\/p>\n<p class=\"SbarParamid\">An interesting use of the <b>&minus;like<\/b> operator is to search the elements of an array. If the <b>$b<\/b> variable is used to hold the array <b>&ldquo;This&rdquo;,&rdquo;is&rdquo;,&rdquo;a&rdquo;,&rdquo;string&rdquo;<\/b> , and the <b>&minus;like<\/b> operator searches the array for <b>&ldquo;*ring*&rdquo;<\/b>, every match for the wildcard pattern is returned&mdash;not just a True or False answer.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $b = &quot;This&quot;,&quot;is&quot;,&quot;a&quot;,&quot;string&quot;<br \/> PS C:\\&gt; $b -like &quot;*ring*&quot;<br \/> string<\/p>\n<h2>The &minus;match operator<\/h2>\n<p class=\"SbarParamid\">The <b>&minus;match<\/b> operator is used to perform a regular expression pattern match. When the match is found, <b>True<\/b> is returned. If a match is not found, <b>False<\/b> is returned. If the <b>$a<\/b> variable is assigned the value <b>&quot;This is a string&quot;<\/b>, and the <b>&minus;match<\/b> operator is used to look for the value of <b>&ldquo;is&rdquo;<\/b>, the pattern is a match, and <b>True<\/b> is returned as shown here.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;This is a string&quot;<br \/> PS C:\\&gt; $a -match &quot;is&quot;<br \/> True<\/p>\n<p class=\"SbarParamid\">More complex match patterns can be used. For example, the <b>\\w<\/b> character is used with regular expressions to look for any white space, such as a space before or after a letter. When the <b>$a<\/b> variable is used to hold the string <b>&quot;This is a string&quot;<\/b> and the regular expression pattern <b>[\\w a \\w]<\/b> is used, a match will be returned if the letter <b>a<\/b> is found with a space in front and a space behind the letter, as shown here.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;This is a string&quot;<br \/> PS C:\\&gt; $a -match &quot;[\\w a \\w]&quot;<br \/> True<\/p>\n<p class=\"SbarParamid\">What about matching with an array? If the <b>$c<\/b> variable is used to hold the array&nbsp; <b>&quot;This&quot;,&quot;is&quot;,&quot;a&quot;,&quot;string&quot;<\/b> and the regular expression pattern match <b>&ldquo;is&rdquo;<\/b> 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.<\/p>\n<p class=\"SbarCodeBlock\" style=\"margin-left:30px\">PS C:\\&gt; $c = &quot;This&quot;,&quot;is&quot;,&quot;a&quot;,&quot;string&quot;<br \/> PS C:\\&gt; $c -match &quot;is&quot;<br \/> This<br \/> is<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: &nbsp;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. When you are searching for a string, you [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[331,460,3,4,45],"class_list":["post-2171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-best-practices","tag-powershell-4","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: &nbsp;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. When you are searching for a string, you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2171"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2171\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}