<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dreaming in PowerShell : Regular Expression, path</title><link>http://powershell.com/cs/blogs/tobias/archive/tags/Regular+Expression/path/default.aspx</link><description>Tags: Regular Expression, path</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Regular Expressions Are Your Friend (Part 2)</title><link>http://powershell.com/cs/blogs/tobias/archive/2011/11/14/regular-expressions-are-your-friend-part-2.aspx</link><pubDate>Mon, 14 Nov 2011 10:37:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:13228</guid><dc:creator>Tobias</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://powershell.com/cs/blogs/tobias/rsscomments.aspx?PostID=13228</wfw:commentRss><comments>http://powershell.com/cs/blogs/tobias/archive/2011/11/14/regular-expressions-are-your-friend-part-2.aspx#comments</comments><description>&lt;p&gt;In &lt;a target="_blank" href="http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular-expressions-are-your-friend-part-1.aspx"&gt;Part 1&lt;/a&gt;, we used &lt;strong&gt;Regular Expressions&lt;/strong&gt; to extract useful information from noise text. That was pretty cool, and we used the operator &lt;strong&gt;-match&lt;/strong&gt;, the automatic PowerShell variable &lt;strong&gt;$matches&lt;/strong&gt; and the powerful &lt;strong&gt;Get-Matches&lt;/strong&gt; function.&lt;/p&gt;
&lt;p&gt;Today, we&amp;#39;ll use Regular Expressions again, but this time they are used&amp;nbsp;to split text. &lt;/p&gt;
&lt;h3&gt;Chopping Text Into Pieces&lt;/h3&gt;
&lt;p&gt;Regular Expressions can be your &lt;strong&gt;butchers&amp;#39; knife to split text&lt;/strong&gt; into pieces. Why on earth would you like do that, though? &lt;/p&gt;
&lt;p&gt;For example, to process a comma-separated list of values. Have a look:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39;comma,separated,list&amp;#39; -split &amp;#39;,&amp;#39;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here, the operator &lt;strong&gt;-split&lt;/strong&gt; takes the text on its left side and chops the text up at each comma it finds. Yes, Regular Expressions do not need to be monsters. They can be very small. In this case, our Regular Expression simply is the comma - and represents a comma. It is - uh - a placeholder for - a comma. Easy enough. &lt;/p&gt;
&lt;p&gt;You now could pipe the results to &lt;strong&gt;Foreach-Object&lt;/strong&gt; and do stuff for all the elements in your comma-separated list:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;$computers = &amp;#39;pc1,pc2,server12,serverAB&amp;#39;&lt;br /&gt;$computers -split &amp;#39;,&amp;#39; | Foreach-Object { &amp;quot;I am doing something with $_&amp;quot; }&lt;br /&gt;I am doing something with pc1&lt;br /&gt;I am doing something with pc2&lt;br /&gt;I am doing something with server12&lt;br /&gt;I am doing something with serverAB&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;$computers -split &amp;#39;,&amp;#39; | Foreach-Object {&amp;nbsp;Get-WMIObject Win32_BIOS -computername $_&amp;nbsp;}&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You can also use splitting to take apart &lt;strong&gt;path names&lt;/strong&gt;. Here, the pattern is the backslash (if you want the parts of the path) or a dot (if you are after the &lt;strong&gt;file extension&lt;/strong&gt;). This time, however, the pattern to use is not as straight-forward. Both the backslash and the dot are &lt;strong&gt;special characters in Regular Expressions&lt;/strong&gt;. So if you really mean a backslash or a dot, you need to &lt;strong&gt;escape them&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; &amp;#39;c:\windows\file.txt&amp;#39; -split &amp;#39;\.&amp;#39;&lt;br /&gt;c:\windows\file&lt;br /&gt;txt&lt;br /&gt;PS&amp;gt; (&amp;#39;c:\windows\file.txt&amp;#39; -split &amp;#39;\.&amp;#39;)[-1]&lt;br /&gt;txt&lt;br /&gt;PS&amp;gt; &amp;#39;c:\windows\file.txt&amp;#39; -split &amp;#39;\\&amp;#39;&lt;br /&gt;c:&lt;br /&gt;windows&lt;br /&gt;file.txt&lt;br /&gt;PS&amp;gt; (&amp;#39;c:\windows\file.txt&amp;#39; -split &amp;#39;\\&amp;#39;)[-1]&lt;br /&gt;file.txt&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Escaping Regular Expressions&lt;/h3&gt;
&lt;p&gt;As you have just discovered, sometimes it is necessary to escape characters in Regular Expressions so they are taken literally. &lt;/p&gt;
&lt;p&gt;Fortunately, there is a way to &lt;strong&gt;automatically escape characters&lt;/strong&gt;. If you aren&amp;#39;t sure if something you mean literally should indeed be escaped or not, try using this trick:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; [Regex]::Escape(&amp;#39;c:\folder\file.txt&amp;#39;)&lt;br /&gt;c:\\folder\\file\.txt&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It takes the text you provide and returns it in escaped form so Regular Expressions wouldn&amp;#39;t treat anything in that text as special characters.&lt;/p&gt;
&lt;h3&gt;Splitting Without Consuming&lt;/h3&gt;
&lt;p&gt;By default, when you chop up text using &lt;strong&gt;-split&lt;/strong&gt;, the split character(s) are &amp;quot;consumed&amp;quot;, so they are gone after the split. &lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; $text = &amp;#39;This text gets splitted, and the delimiter is by default consumed, as you can see&amp;#39;&lt;br /&gt;PS&amp;gt; $text -split &amp;#39;,&amp;#39;&lt;br /&gt;This text gets splitted&lt;br /&gt;&amp;nbsp;and the delimiter is by default consumed&lt;br /&gt;&amp;nbsp;as you can see&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;With look-behind expressions, you can make sure that the splitting pattern is not consumed:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; $text -split &amp;#39;(?&amp;lt;=, )&amp;#39;&lt;br /&gt;This text gets splitted,&lt;br /&gt;and the delimiter is by default consumed,&lt;br /&gt;as you can see&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Basically, &amp;quot;?&amp;lt;=&amp;quot; tells the RegEx engine to split &lt;em&gt;right after&lt;/em&gt; the splitting character. Splitting without consuming can become important as you&amp;#39;ll see in a second.&lt;/p&gt;
&lt;h3&gt;Separating Hex-Pairs&lt;/h3&gt;
&lt;p&gt;Let&amp;#39;s assume you have a long &lt;strong&gt;list of hexadecimal pairs &lt;/strong&gt;and would like to &lt;strong&gt;convert&lt;/strong&gt; that list back to &lt;strong&gt;decimals&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39;00AB1CFFAB1034&amp;#39; &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To do that, you would somehow have to split after every second character and without consuming because you do not want to loose any information. Here is how you solve this puzzle:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; &amp;#39;00AB1CFFAB1034&amp;#39; -split &amp;#39;(?&amp;lt;=\G[0-9a-f]{2})(?=.)&amp;#39;&lt;br /&gt;00&lt;br /&gt;AB&lt;br /&gt;1C&lt;br /&gt;FF&lt;br /&gt;AB&lt;br /&gt;10&lt;br /&gt;34&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now you can pipe the pairs to &lt;strong&gt;Foreach-Object&lt;/strong&gt; and convert them to decimals or bytes or whatever you may need:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; &amp;#39;00AB1CFFAB1034&amp;#39; -split &amp;#39;(?&amp;lt;=\G[0-9a-f]{2})(?=.)&amp;#39; | &lt;br /&gt;&amp;nbsp; Foreach-Object { [System.Convert]::ToInt32($_, 16) }&lt;br /&gt;0&lt;br /&gt;171&lt;br /&gt;28&lt;br /&gt;255&lt;br /&gt;171&lt;br /&gt;16&lt;br /&gt;52&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&amp;gt; &amp;#39;00AB1CFFAB1034&amp;#39; -split &amp;#39;(?&amp;lt;=\G[0-9a-f]{2})(?=.)&amp;#39; | &lt;br /&gt;&amp;nbsp; Foreach-Object { [System.Convert]::ToByte($_, 16) }&lt;br /&gt;0&lt;br /&gt;171&lt;br /&gt;28&lt;br /&gt;255&lt;br /&gt;171&lt;br /&gt;16&lt;br /&gt;52&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Whew, wait a minute! This time, the Regular Expression wasn&amp;#39;t as simple anymore. Right. &lt;strong&gt;Regular Expressions aren&amp;#39;t hated without a reason&lt;/strong&gt;. Sometimes they become tricky little monsters with mind-blowing &lt;strong&gt;lookaheads&lt;/strong&gt;, &lt;strong&gt;lookbehinds&lt;/strong&gt; and &lt;strong&gt;lookarounds&lt;/strong&gt;. But is that a reason not to use them? No! We all use microwaves to make popcorn, but do we care about the physics? I don&amp;#39;t.&lt;/p&gt;
&lt;p&gt;So always remember: you do not need to care about Regular Expression mechanics once you have one&amp;nbsp;that does what you&amp;nbsp;want it to do. So once you have a Regular Expression that splits Hex-Pairs, you are good. Simply use it. &lt;/p&gt;
&lt;p&gt;Instead of spending frustrating hours of crafting complex RegEx patterns, think &amp;quot;code reuse&amp;quot; and visit a search engine like google.Look for &amp;quot;RegEx &amp;lt;Pattern&amp;gt;&amp;quot;, for example &amp;quot;RegEx IPAddress&amp;quot;. Chances are someone else has already done the dirty job for you, and all you need to do is &lt;strong&gt;copy and paste the RegEx pattern&lt;/strong&gt;. &lt;/p&gt;
&lt;p&gt;If you do want to digest patterns and really understand what is going on, there are plenty of &lt;strong&gt;great RegEx tutorial sites&lt;/strong&gt;. This one explains lookbehinds, lookaheads and lookarounds and helps you understand the previous example: &lt;a target="_blank" href="http://www.regular-expressions.info/lookaround.html"&gt;http://www.regular-expressions.info/lookaround.html&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;What&amp;rsquo;s Next?&lt;/h3&gt;
&lt;p&gt;We covered -match and -split, but there&amp;#39;s a third family member: -replace. So next time we&amp;#39;ll continue talking about RegEx and look how you can replace things, update IP lists and more. Meanwhile, if you have spare time, go visit some of the plenty sites that focus on RegEx, and try and refine your understanding of RegEx placeholders, anchors and quantifiers. Stay tuned!&lt;/p&gt;
&lt;p&gt;Tobias&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Microsoft MVP PowerShell Germany&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;P.S.&lt;br /&gt;If you live in Germany or other parts of Europe and your company would like to set up a truly great PowerShell training, just contact me! I regularly train mid- to large-size companies. Trainings are always a blast with tons of real-world-examples and solutions. Here&amp;#39;s how to get in touch with me: &lt;a href="mailto:tobias.weltner@scriptinternals.de"&gt;&lt;span style="color:#3366cc;"&gt;tobias.weltner@scriptinternals.de&lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://powershell.com/cs/aggbug.aspx?PostID=13228" width="1" height="1"&gt;</description><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Split/default.aspx">Split</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/path/default.aspx">path</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Convert/default.aspx">Convert</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/-match/default.aspx">-match</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Regular+Expression/default.aspx">Regular Expression</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/RegEx/default.aspx">RegEx</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/-split/default.aspx">-split</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/splitting/default.aspx">splitting</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/hexadecimal/default.aspx">hexadecimal</category></item></channel></rss>