{"id":6791,"date":"2019-07-03T11:02:48","date_gmt":"2019-07-03T15:02:48","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=6791"},"modified":"2019-07-03T11:02:56","modified_gmt":"2019-07-03T15:02:56","slug":"capturing-names-with-powershell-and-regular-expressions","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/","title":{"rendered":"Capturing Names with PowerShell and Regular Expressions"},"content":{"rendered":"<p>As you continue to learn and embrace PowerShell, you will eventually meet regular expressions. Hopefully many of you already have some fundamental knowledge. if not, the first place to start is by reading the help topic, about_regular_expressions In this article, I'm gong to introduce you to an advanced regular expression topic \u2013 named captures. I'll admit that when I first learned this topic it made my head spin. But hopefully I can slow down the merry-go-round.<\/p>\n<p>Let's begin with a string like this.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t = \"2019-06-21 17:12:31Z : 172.16.1.123 [ Begin process data ]\"\n<\/pre>\n<p>This is something you might have in a log file. .Since PowerShell's strength comes from its ability to work with objects, it might be easier to transform the log file into a collection of objects.<\/p>\n<h2>Start with Patterns<\/h2>\n<p>In order for this to work, and for you to use regular expressions, you have to know what your data will look like and it must be consistent and predictable. In this sample, I have a datetime string, an IPv4 address and then a message inside the brackets. The first step is to create a regular expression pattern that matches on these different elements. Let's begin with the datetime.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t -match \"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}Z\"\n<\/pre>\n<p>The pattern says, find something that starts with exactly 4 digits. The \\d means a digit and the {4} means exactly 4 digits. Then there is a dash followed by exactly 2 digits (\\d{2}) another dash and 2 digits. That should give me the date. But there's a bit more. I need to capture the space (\\s) and then I have a pattern to match the time which by now you should recognize. The end is a literal Z<\/p>\n<p>.<a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Matching a datetime string with regular expressions and PowerShell\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png\" alt=\"Matching a datetime string with regular expressions and PowerShell\" width=\"644\" height=\"192\" border=\"0\" \/><\/a><\/p>\n<p>As you can see, this matches on exactly what I want. By the way, there is often more than one pattern you could write. I'm trying to stick with simple and clear patterns. Regular expressions can be cryptic enough!<\/p>\n<p>Next, I need to match the IP address.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t -match \"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\"\n<\/pre>\n<p>The pattern says start with at least 1 digit and no more than 3. (\\d{1,3}) followed by a literal period. (\\.). The period is a special regular expression character that means anything. The \\ is the escape character which tells PowerShell to look for a literal period. This pattern repeats for the remaining octets.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Matching an IP address with regular expressions and PowerShell\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-2_thumb.png\" alt=\"Matching an IP address with regular expressions and PowerShell\" width=\"644\" height=\"211\" border=\"0\" \/><\/a><\/p>\n<p>This pattern doesn't validate the address, just that it <em>looks<\/em> like an IP address. There are patterns that are more restrictive but I didn't want to add any more complexity. And in my situation, I know the address will be valid.<\/p>\n<p>Last is the text between the square brackets.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t -match \"\\[.*\\]\"\n<\/pre>\n<p>The bracket is a special regular expression character so I need to escape it. I'm then asking to match on multiple instances (*) of any character (.).<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Matching text with regular expressions and PowerShell\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-3_thumb.png\" alt=\"Matching text with regular expressions and PowerShell\" width=\"644\" height=\"223\" border=\"0\" \/><\/a><\/p>\n<h2>Define the Names<\/h2>\n<p>Once you have matching patterns, you can define them with a name. The general layout looks like this:<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">(?&lt;capture-name&gt;Your-Pattern)\n<\/pre>\n<p>The parentheses are key. My datetime pattern can be defined as a named capture:<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">(?&lt;date&gt;\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}Z)\n<\/pre>\n<p>Here is a pattern that describes the entire line of text from beginning to end.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">[regex]$rx = '(?&lt;date&gt;\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}Z)\\s:\\s(?&lt;ip&gt;\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s\\[\\s(?&lt;status&gt;.*)\\]'\n<\/pre>\n<p>I've tweaked the last pattern to not include the brackets. There are other advanced regular expression techniques I could have used but I kept it simple. The important thing to note is that I am defining a $rx as a special kind of object. This regex object allows me to do much more than simply using the \u2013match operator. In this situation, I can use it to look for matches.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Matching with a regex object\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-4_thumb.png\" alt=\"Matching with a regex object\" width=\"644\" height=\"207\" border=\"0\" \/><\/a><\/p>\n<p>This is good. But how do the names come into play?<\/p>\n<h2>Using Named Captures in PowerShell<\/h2>\n<p>In the match, you can see a Groups property. This is where the named captures can be found.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Regex matched groups\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-5_thumb.png\" alt=\"Regex matched groups\" width=\"561\" height=\"484\" border=\"0\" \/><\/a><\/p>\n<p>Using PowerShell it is easy to get only the named captures by skipping the first matched group.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Getting named capture values\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-6_thumb.png\" alt=\"Getting named capture values\" width=\"644\" height=\"171\" border=\"0\" \/><\/a><\/p>\n<p>That almost looks like an object! it doesn't take much more to create an actual object.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$m.groups | Select-Object -Skip 1 | foreach-object -begin { $h = @{}  } -process {\n    $h.Add($_.name,$_.value.trim())\n} -end {\n    [pscustomobject]$h\n}\n<\/pre>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"float: right; display: inline; background-image: none;\" title=\"Creating a custom object from a regex capture\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-7_thumb.png\" alt=\"Creating a custom object from a regex capture\" width=\"644\" height=\"240\" align=\"right\" border=\"0\" \/><br \/>\n<\/a><\/p>\n<p>Although, it would be nicer if the date was a datetime object instead of a string. Here's an alternative assuming you know the order of your named captures.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$o = [pscustomobject]@{\n    Date = $m.groups[1].value -as [datetime]\n    IPAddress = $m.groups[2].value -as [ipaddress]\n    Status = $m.groups[3].value \n}\n<\/pre>\n<p>As you can see this is an object.<\/p>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Creating a custom PowerShell object from a named regex capture\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-8_thumb.png\" alt=\"Creating a custom PowerShell object from a named regex capture\" width=\"644\" height=\"439\" border=\"0\" \/><\/a><\/p>\n<p>With this code, I can write a script to process each line of the log file, creating a custom object. That makes it much easier to filter, sort or do whatever else I need to do with the data.<\/p>\n<h2>ConvertFrom-Text<\/h2>\n<p>Or how about an easier way? Now that you know how to create a regular expression pattern using named captures, you can use the <a href=\"https:\/\/github.com\/jdhitsolutions\/PSScriptTools\/blob\/master\/docs\/ConvertFrom-Text.md\" target=\"_blank\" rel=\"noopener noreferrer\">ConvertFrom-Text<\/a> command in my PSScriptTools module which you can install from the PowerShell Gallery. With this command you can create code to turn any text output into PowerShell objects.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$c = \"(?&lt;Protocol&gt;\\w{3})\\s+(?&lt;LocalIP&gt;(\\d{1,3}\\.){3}\\d{1,3}):(?&lt;LocalPort&gt;\\d+)\\s+(?&lt;ForeignIP&gt;.*):(?&lt;ForeignPort&gt;\\d+)\\s+(?&lt;State&gt;\\w+)?\"\nnetstat -an | select -skip 4 | convertfrom-text $c | where-object {$_.LocalIP \u2013ne '0.0.0.0'} | format-table \u2013autosize\n<\/pre>\n<p><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"Converting text to objects\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-9_thumb.png\" alt=\"Converting text to objects\" width=\"644\" height=\"253\" border=\"0\" \/><\/a><\/p>\n<p>Once you have at least some basic regular expression skills you'll find yourself using it often. And as with any new language, the more more you use it the more fluent you will become.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you continue to learn and embrace PowerShell, you will eventually meet regular expressions. Hopefully many of you already have some fundamental knowledge. if not, the first place to start is by reading the help topic, about_regular_expressions In this article, I&#8217;m gong to introduce you to an advanced regular expression topic \u2013 named captures. I&#8217;ll&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Capturing Names with #PowerShell and Regular Expressions","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[534,250,540],"class_list":["post-6791","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-powershell","tag-regular-expressions","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Capturing Names with PowerShell and Regular Expressions &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"Parsing text with regular expressions and named captures is easier than you think with PowerShell. And once you understand, there is no end to what you can do.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Capturing Names with PowerShell and Regular Expressions &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Parsing text with regular expressions and named captures is easier than you think with PowerShell. And once you understand, there is no end to what you can do.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-03T15:02:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-03T15:02:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Capturing Names with PowerShell and Regular Expressions\",\"datePublished\":\"2019-07-03T15:02:48+00:00\",\"dateModified\":\"2019-07-03T15:02:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/\"},\"wordCount\":807,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/regex-match-1_thumb.png\",\"keywords\":[\"PowerShell\",\"Regular Expressions\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/\",\"name\":\"Capturing Names with PowerShell and Regular Expressions &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/regex-match-1_thumb.png\",\"datePublished\":\"2019-07-03T15:02:48+00:00\",\"dateModified\":\"2019-07-03T15:02:56+00:00\",\"description\":\"Parsing text with regular expressions and named captures is easier than you think with PowerShell. And once you understand, there is no end to what you can do.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/regex-match-1_thumb.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/regex-match-1_thumb.png\",\"width\":644,\"height\":192},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/6791\\\/capturing-names-with-powershell-and-regular-expressions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Capturing Names with PowerShell and Regular Expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Capturing Names with PowerShell and Regular Expressions &#8226; The Lonely Administrator","description":"Parsing text with regular expressions and named captures is easier than you think with PowerShell. And once you understand, there is no end to what you can do.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/","og_locale":"en_US","og_type":"article","og_title":"Capturing Names with PowerShell and Regular Expressions &#8226; The Lonely Administrator","og_description":"Parsing text with regular expressions and named captures is easier than you think with PowerShell. And once you understand, there is no end to what you can do.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/","og_site_name":"The Lonely Administrator","article_published_time":"2019-07-03T15:02:48+00:00","article_modified_time":"2019-07-03T15:02:56+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Capturing Names with PowerShell and Regular Expressions","datePublished":"2019-07-03T15:02:48+00:00","dateModified":"2019-07-03T15:02:56+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/"},"wordCount":807,"commentCount":1,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png","keywords":["PowerShell","Regular Expressions","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/","name":"Capturing Names with PowerShell and Regular Expressions &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png","datePublished":"2019-07-03T15:02:48+00:00","dateModified":"2019-07-03T15:02:56+00:00","description":"Parsing text with regular expressions and named captures is easier than you think with PowerShell. And once you understand, there is no end to what you can do.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-1_thumb.png","width":644,"height":192},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Capturing Names with PowerShell and Regular Expressions"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2211,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2211\/powershell-scripting-with-validatepattern\/","url_meta":{"origin":6791,"position":0},"title":"PowerShell Scripting with [ValidatePattern]","author":"Jeffery Hicks","date":"April 19, 2012","format":false,"excerpt":"I've been writing about a number of parameters attributes you can include in your PowerShell scripting to validate parameter values. Today I want to cover using a regular expression pattern to validate a parameter value. I'm going to assume you have a rudimentary knowledge of how to use regular expressions\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/squarepattern-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3744,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3744\/friday-fun-find-file-locking-process-with-powershell\/","url_meta":{"origin":6791,"position":1},"title":"Friday Fun: Find File Locking Process with PowerShell","author":"Jeffery Hicks","date":"March 14, 2014","format":false,"excerpt":"I was asked on Twitter this morning about a way to find out what process has a lock on a given file. I'm not aware of any PowerShell cmdlet that can do that but I figured there had to be a .NET way and if I could find a code\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"handle","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/03\/handle-300x90.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3727,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3727\/find-and-replace-text-with-powershell\/","url_meta":{"origin":6791,"position":2},"title":"Find and Replace Text with PowerShell","author":"Jeffery Hicks","date":"February 27, 2014","format":false,"excerpt":"I've just finished up a series of tweets with a follower who had a question about finding and replacing a bit of data in a text file. In his case it was a web.config file but it really could be any text file that you can view in PowerShell. In\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"magnifying-glass","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/11\/magnifying-glass.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3627,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3627\/friday-fun-with-rest-regex-and-replacements\/","url_meta":{"origin":6791,"position":3},"title":"Friday Fun with REST, Regex and Replacements","author":"Jeffery Hicks","date":"January 24, 2014","format":false,"excerpt":"I just love using the web cmdlets that were introduced in PowerShell 3.0. One of the cmdlets I use the most is Invoke-RESTMethod. This isn't because I'm dealing with sites that offer REST-ful services, but rather I like that the cmdlet does all the heavy lifting for me when I\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"computereye","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/computereye-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":8724,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","url_meta":{"origin":6791,"position":4},"title":"Discovering Aliases with the PowerShell AST","author":"Jeffery Hicks","date":"December 15, 2021","format":false,"excerpt":"I've been working on a new PowerShell module that incorporates code from a few of my recent posts on converting PowerShell scripts and functions to files. I even whipped up a script, think of it as a meta-script, to create the module using the commands that I am adding to\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3881,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3881\/scraping-sysinternals\/","url_meta":{"origin":6791,"position":5},"title":"Scraping Sysinternals","author":"Jeffery Hicks","date":"June 16, 2014","format":false,"excerpt":"Recently I was conversing with someone about my PowerShell code that downloads tools from the live Sysinternals site. If you search the Internet, you'll find plenty of ways to achieve the same goal. But we were running into a problem where PowerShell was failing to get information from the site.\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"download-thumb","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/06\/download-thumb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=6791"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6791\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=6791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=6791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=6791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}