{"id":80245,"date":"2016-09-30T00:01:07","date_gmt":"2016-09-30T07:01:07","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80245"},"modified":"2019-02-18T09:10:26","modified_gmt":"2019-02-18T16:10:26","slug":"powershell-regex-crash-course-part-1-of-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-regex-crash-course-part-1-of-5\/","title":{"rendered":"PowerShell regex crash course \u2013 Part 1 of 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Thomas Rayner, Microsoft Cloud and\u00a0Datacenter Management MVP, shows the basics of working with regular expressions in PowerShell.<\/p>\n<p>Hello! I\u2019m Thomas Rayner, a proud Cloud and\u00a0Datacenter Management Microsoft MVP, filling in for The Scripting Guy! this week. You can find me on Twitter (<a target=\"_blank\" href=\"http:\/\/twitter.com\/MrThomasRayner\">@MrThomasRayner<\/a>) or posting on my blog, <a target=\"_blank\" href=\"http:\/\/workingsysadmin.com\">workingsysadmin.com<\/a>. This week, I\u2019m presenting a five-part crash course about how to use regular expressions in PowerShell. Regular expressions are sequences of characters that define a search pattern, mainly for use in pattern matching with strings. Regular expressions are extremely useful to\u00a0extract information from text such as log files or documents. This isn\u2019t meant to be a comprehensive series but rather, just as the name says, a crash course. So buckle up!<\/p>\n<p>Many people are intimidated by regular expressions, or \u201cregex\u201d. If you see something like <strong>&#8216;(\\d{1,3}\\.){3}(\\d{1,3})&#8217;<\/strong> and your eyes start glazing over, don\u2019t worry. By the end of this series, you\u2019ll have the skills to identify that pattern matches IP addresses. For the uninitiated, big strings of seemingly random characters appear indecipherable, but regex is an incredibly powerful tool that any PowerShell pro needs to have a grip on.<\/p>\n<p>Today, I\u2019m explaining some of the basics. Regex is a really big topic, and a proper \u201cIntro to regex\u201d post series could probably be broken into five parts on its own. This, however, isn\u2019t an introduction to regex series, it\u2019s a regex crash course.<\/p>\n<p>If you\u2019ve dabbled in PowerShell before, you\u2019ve probably already used rudimentary regex, maybe without even realizing it. Have you ever run something like this?<\/p>\n<p style=\"padding-left: 60px\"><code><em>$ArrayOfStuff = @(\u2018somethingone\u2019,\u2019somethingtwo\u2019,\u2019noway\u2019)<\/em>\/<\/code><\/p>\n<p style=\"padding-left: 60px\"><code><em>$ArrayOfStuff | Where-Object { $_ -match \u2018something\u2019 }<\/em><\/code><\/p>\n<p>This simple example will return the first two items in the array because they match the pattern \u201c<em>something<\/em>\u201d. The third array item is not returned because the pattern \u201c<em>something<\/em>\u201d isn\u2019t found in the item anywhere. In other words, the first two items of the array contain a specific pattern of characters that we are interested in, but the third one doesn\u2019t. That sounds like regex!<\/p>\n<p>You can also use the <code>\u2013match<\/code> operator without putting it inside <code>where-object<\/code>. In this case, you can test whether a given pattern exists within a string.<\/p>\n<p style=\"padding-left: 60px\"><code>\u2018<em>somethingone\u2019 \u2013match \u2018something\u2019\u00a0 \u00a0\u00a0 #returns True<\/em><\/code><\/p>\n<p style=\"padding-left: 60px\"><code>\u2018<em>somethingone\u2019 \u2013match \u2018pickles\u2019 \u00a0 \u00a0 \u00a0 #returns False<\/em><\/code><\/p>\n<p>Another operator that some people don\u2019t realize works with regex is <code>\u2013replace<\/code>. With it, you can do things like the following example. The matching principle works the same as the <code>\u2013match<\/code> operator, but as you might guess by the name, <code>-replace<\/code> does more than just report if a pattern exists in a string.<\/p>\n<p style=\"padding-left: 60px\"><code>\u2018<em>Here is a simple string.\u2019 \u2013replace \u2018simple string\u2019,\u2019string that got something replaced<\/em>\u2019<\/code><\/p>\n<p>You\u2019ll get \u201c<em>Here is a string that got something replaced.<\/em>\u201d back from this. What happened is <code>\u2013replace<\/code> matched the pattern \u201c<em>simple string<\/em>\u201d and replaced it with \u201c<em>string that got something replaced<\/em>\u201d. Neat, right?<\/p>\n<p>What if you had a more complicated task? How about if you have a string that\u2019s a file name, and you want to know if it starts with a specific letter and ends in a specific file extension? For instance, does <em>something.txt<\/em> start with an <em>s<\/em> and end with <em>.txt<\/em>? What about <em>notthis.txt<\/em>? Well for this, we need to introduce <em>quantifiers<\/em>.<\/p>\n<p>In PowerShell regex, there are three quantifiers: <em>*<\/em>, <em>+<\/em>, and <em>?<\/em> (star, plus and question mark for those who are new). They all mean different things in regex.<\/p>\n<table width=\"40%\" border=\"1\">\n<tbody>\n<tr>\n<td>*<\/td>\n<td>Means zero or more of something<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>Means one or more of something<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Means zero or one of something<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So, if we want to examine the file names from the previous example, the easiest thing to do is use the star character.<\/p>\n<p style=\"padding-left: 60px\"><code><em>\u2018something.txt\u2019 \u2013match \u2018s*.txt\u2019 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #returns True<\/em><\/code><\/p>\n<p style=\"padding-left: 60px\"><code><em>\u2018notthis.txt\u2019 \u2013match \u2018s*.txt\u2019\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #returns False<\/em><\/code><\/p>\n<p>In this example, the regex looks for the pattern \u201c<em>s followed by zero or more of anything, followed by .txt<\/em>\u201d. \u2018<em>something.txt<\/em>\u2019 indeed starts with an <em>s<\/em>, has zero or more of anything (\u201c<em>omething<\/em>\u201d) and then has a <em>.txt<\/em> so it matches. \u2018<em>notthis.txt<\/em>\u2019 doesn\u2019t start with an <em>s<\/em>, so it fails to match the pattern.<\/p>\n<p>Cool, right? We&#8217;ll dig into the other quantifiers later when we&#8217;re getting deeper into regex. Stay tuned for next week\u2019s post where I&#8217;ll be explaining special characters!<\/p>\n<p>Thanks, Thomas!\u00a0 We\u2019ll be looking forward to reading up on this next week.<\/p>\n<p>I invite you to follow the Scripting Guys on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to them at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Thomas Rayner, Microsoft Cloud and\u00a0Datacenter Management MVP, shows the basics of working with regular expressions in PowerShell. Hello! I\u2019m Thomas Rayner, a proud Cloud and\u00a0Datacenter Management Microsoft MVP, filling in for The Scripting Guy! this week. You can find me on Twitter (@MrThomasRayner) or posting on my blog, workingsysadmin.com. This week, I\u2019m presenting a [&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":[568,686,685,641],"tags":[56,652,45],"class_list":["post-80245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-regex","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Thomas Rayner, Microsoft Cloud and\u00a0Datacenter Management MVP, shows the basics of working with regular expressions in PowerShell. Hello! I\u2019m Thomas Rayner, a proud Cloud and\u00a0Datacenter Management Microsoft MVP, filling in for The Scripting Guy! this week. You can find me on Twitter (@MrThomasRayner) or posting on my blog, workingsysadmin.com. This week, I\u2019m presenting a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80245","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=80245"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80245\/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=80245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}