{"id":80345,"date":"2016-10-21T11:59:54","date_gmt":"2016-10-21T18:59:54","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80345"},"modified":"2019-02-18T09:10:24","modified_gmt":"2019-02-18T16:10:24","slug":"powertip-how-to-use-regular-expressions-to-split-a-string-without-losing-the-character-you-split-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-how-to-use-regular-expressions-to-split-a-string-without-losing-the-character-you-split-on\/","title":{"rendered":"PowerTip: How to use regular expressions to split a string without losing the character you split on"},"content":{"rendered":"<p><strong>Summary<\/strong>: Cloud and Datacenter Management MVP, Thomas Rayner, shows how to split a string without losing the character you split on.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> I\u2019m splitting this file name <em>some file.txt<\/em> into its name and extension by going <strong>\u201csome file.txt\u201d \u2013split \u201c.\u201d<\/strong>. It\u2019s giving me <em>some file<\/em> and <em>txt<\/em>, but I want to keep the dot and get <em>.txt<\/em> instead. How can I do this?<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> You can split on a regex lookahead and split on the space between characters where the character on the right is a dot<\/p>\n<p style=\"padding-left: 90px\"><code>'some file.txt' -split '(?=\\.)'<\/code><\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"The Doctor\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how to split a string without losing the character you split on. I\u2019m splitting this file name some file.txt into its name and extension by going \u201csome file.txt\u201d \u2013split \u201c.\u201d. It\u2019s giving me some file and txt, but I want to keep the dot and get [&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,639,687,641],"tags":[56,356,652,45],"class_list":["post-80345","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powertip","category-regular-expressions","category-windows-powershell","tag-guest-blogger","tag-powertip","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how to split a string without losing the character you split on. I\u2019m splitting this file name some file.txt into its name and extension by going \u201csome file.txt\u201d \u2013split \u201c.\u201d. It\u2019s giving me some file and txt, but I want to keep the dot and get [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80345","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=80345"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80345\/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=80345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}