<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body style='font-family: Verdana,Geneva,sans-serif'>
<p>Hi all,</p>
<p style="padding-left: 30px;">The following is my report for HW#N.</p>
<p style="padding-left: 30px;">&nbsp; &nbsp; &nbsp;Last Thursday, I went to the meet-up &ldquo;A gentle intro to regex&rdquo; with my friends and learned lots of extra important tips for the regex. The main purpose of the event is that giving us a simple but impressive principle to develop with regex in good manner. <br />&nbsp; &nbsp; &nbsp;At the beginning of regex party, the lecture introduce the main purpose of regex &ndash; Validate, Route &amp; Matching, Search &amp; Replace(substitution), Parse. As a system administrator, we are always analyzing the status of groups of servers or automating the work of updating or configuring the work environment for team. The regex is fundamentally useful in a system administrator&rsquo;s routine work. We need to use &ldquo;Route &amp; Matching&rdquo; to do analyze, e.g. if we want to write a script to get the basic information of IP, MASK, MAC, it&rsquo;s very convenient for us to implement if applying the regex in our implementation. Search &amp; Replace is also our friend, the typical example is that we always use the vim tools to locate and modify string via regex. However, as the lecture says that the &ldquo;Parse&rdquo; is a limited function.<br />&nbsp; &nbsp; &nbsp;Then, the lecture moved on the topic and introduces the most interested part of the event. He gives us an example if we want to catch the video id (e.g. dQw4w9WgXcQ) and the url is: <span style="text-decoration: underline;">https://youtube.com/watch?v=dQw4w9WgXcQ</span>. There&rsquo;re lots of way to match the specified string. However, we should obey an important regulation to write a comfortable and elegant regex.<br />1: match only what's necessary<br />E.g. url.match(<em>/.+:\/\/.+\/.+v?=(\w+)/)</em>.pop()<br /> we only match what&rsquo;s necessary here and no effort to match the unnecessary words.<br /> The ugly one could be <br /> <em>/(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&amp;?v\=))|youtu\.be\/)([a-zA-Z0- 9_-]{6,11})/g</em>;</p>
<p style="padding-left: 30px;">2: your expression should be good at doing a single task<br />&nbsp; &nbsp; We could split a complicated string into several trunks of simple and easy manipulated string which means our regex could be more simple, clear and easy to understand. As a system administrator, we&rsquo;re always dealing with the problems with similarity patterns. If we can re-use the snippet of code wrote before in our current project, it save us a large sum of time.</p>
<p style="padding-left: 30px;"><strong>Other interesting questions from the audiences</strong><br />1: How to scale the regex in production environment? E.g. an ugly regex in the loop statement like this:<em> (?:[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*</em><br /><em> | "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]</em><br /><em> | \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")</em><br /><em>@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?</em><br /><em> | \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}</em><br /><em> (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:</em><br /><em> (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]</em><br /><em> | \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)</em><br /><em> \])</em><br />The regex is O(logn) function, so it&rsquo;s better to write a simple and independent regex to deal with a piece of string which can eliminate unnecessary regex matching and save time.<br />2: What the order of translate a string into an array of string via regex? E.g. how about this regex:<em> /.+:\/\/.+\/.+v?=(\w+)/</em><br />The sequence of translate is something like a stack. <br /> ['https://youtube.com/watch?v=dQw4w9WgXcQ', <span style="color: #99cc00;">// index 0</span><br /> 'dQw4w9WgXcQ'] <span style="color: #99cc00;">// index 1</span></p>
<p style="padding-left: 30px;"><strong>Reference:</strong><br />Event slides: http://moimikey.github.io/a-gentle-intro-to-regex/#/<br />github: https://github.com/moimikey/a-gentle-intro-to-regex<br />Challenge: https://gist.github.com/jdaudier/743d2b56091e688702d8</p>
<p>Wen Zhang<br />CWID: 10402152<br />Contact:201-565-6871</p>
</body></html>