<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Spencer Dezart-Smith on Medium]]></title>
        <description><![CDATA[Stories by Spencer Dezart-Smith on Medium]]></description>
        <link>https://medium.com/@spencer.dezart.smith?source=rss-d52388feefd6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*19ZWwR7LspCwUiPzSz92Og.jpeg</url>
            <title>Stories by Spencer Dezart-Smith on Medium</title>
            <link>https://medium.com/@spencer.dezart.smith?source=rss-d52388feefd6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:41:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@spencer.dezart.smith/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My Introduction to Flutter]]></title>
            <link>https://medium.com/carsguide-engineering/my-introduction-to-flutter-16cc8ed58298?source=rss-d52388feefd6------2</link>
            <guid isPermaLink="false">https://medium.com/p/16cc8ed58298</guid>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[mobile-apps]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[learning-and-development]]></category>
            <dc:creator><![CDATA[Spencer Dezart-Smith]]></dc:creator>
            <pubDate>Mon, 25 May 2020 23:57:15 GMT</pubDate>
            <atom:updated>2020-05-25T23:57:15.609Z</atom:updated>
            <content:encoded><![CDATA[<p>Just prior to the beginning of Q2 I was asked if I would be interested in joining a new team focused on building the apps for our two products. I had been working on the web app of one of these products since its inception so I was excited by the opportunity to try something new. The apps would be built using Flutter. What is Flutter you ask???</p><blockquote>Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for <a href="https://flutter.dev/docs">mobile</a>, <a href="https://flutter.dev/web">web</a>, and <a href="https://flutter.dev/desktop">desktop</a> from a single codebase.</blockquote><p>This means that you can use one programming language and one codebase to create two different apps (for iOS and Android). Some of the benefits of using a cross platform development are:</p><ul><li>Faster development. Faster release to market.</li><li>Only one codebase to build and maintain for both platforms.</li><li>Cheaper to produce.</li></ul><p>I wasn’t completely new to this idea. One of my first programming jobs was building a health care app using React-Native. This was an exciting and rewarding project and was relatively easy to pick up as I was experienced in both Javascript and React.</p><p>Flutter is a whole new beast. The underlying language is Dart and the concept behind the SDK is using many, many, many composable widgets all together to build amazing products. Everything is a widget when it comes to Flutter just like everything is an object when we talk about Javascript.</p><p>Anyway, its been a couple weeks since this team was created and as we build we’re slowly working out what is best practice when it comes to writing code and also whats best for overall performance. I wanted to share some of the things I have learnt during this time.</p><h3>A Simple App</h3><p>So <a href="https://dart.dev/guides/language">Dart</a> is an <a href="https://medium.com/run-dart/dart-dartlang-introduction-object-oriented-programming-c3d79d94d303">object oriented language</a> which means that things are generally represented using objects which hold both the data and the methods related to that object. Objects can inherit characteristics from other objects. We create a blueprint for these objects by using a class. Once the blueprint is written we can generate instances for the blueprint which in the case of Flutter, represents a widget!</p><p>Below is the generated project you get when you run flutter create . It is a small app which allows the user to increment a counter by 1 with each button press. Even though it’s small it does a great job at highlighting the two most important building blocks behind Flutters widgets which are the StatefulWidget and StatelessWidget.</p><figure><img alt="Code sample of auto generated flutter app." src="https://cdn-images-1.medium.com/max/838/1*k3re4ad-C-x9ga7hIf8uuA.png" /><figcaption>Auto generated Flutter app.</figcaption></figure><h4>Stateful Widgets</h4><p><a href="https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html">Stateful widgets</a> have their own internal state and the ability to manage that state. Every stateful widget comes with an accompanying state object. You can see this represented by the second class that is generated below the stateful widget. This private class extends Flutter’s State class. An instance of this private class is what’s returned from the createState method which creates the mutable state object for this widget at a given location in the tree. This state object is different to other widgets because it persists through rebuilds of the widget tree but more about that in a bit. The setState method is how a stateful widget updates its state and this is also what causes the build method to trigger a rebuild of the widget tree.</p><h4>Stateless Widgets</h4><p><a href="https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html">Stateless widgets</a> are ‘dumb’ widgets. They either consume data passed into them from a parent or they have their own data which is defined within the widget. They don’t have the ability to update or change this data.</p><p>In the example above there are two user created widgets, MyApp and HomePage . Each of these consume a handful of Flutter’s <a href="https://flutter.dev/docs/development/ui/widgets-intro#basic-widgets">basic widgets</a> such as Container and Text. All the widgets in the file are highlighted in yellow. As we spoke about classes earlier, all these yellow highlights are all just instances of their classes.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FcLtvKDcQYkSRO%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FcLtvKDcQYkSRO%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FcLtvKDcQYkSRO%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="233" frameborder="0" scrolling="no"><a href="https://medium.com/media/8079c8a239f778b500ac56d4bc7e0edd/href">https://medium.com/media/8079c8a239f778b500ac56d4bc7e0edd/href</a></iframe><p>If you want to jump into building your first app check out the Flutter docs <a href="https://flutter.dev/docs/get-started/codelab-web">here</a>. Now let’s look at some of the internal stuff that goes into making Flutter work!</p><h4>Build Method</h4><p>What makes a class a widget is the presence of the required build method. The build method is what is called upon instantiation of a widget and also whenever setState is called. It’s passed a <a href="https://api.flutter.dev/flutter/widgets/BuildContext-class.html">BuildContext</a> as a parameter which provides information about the widgets position in the tree. When this method is called it basically replaces the subtree below it with the widget and subtree it returns. This is how the widget screen is built and subsequently rebuilt throughout the life of the app session.</p><h4>The Widget Tree</h4><p>The widget tree is a representation of all the widgets you, as a developer, have defined to create the structure of your screen. Interestingly enough the widget tree is actually just a bunch of information about the widgets such as padding, alignment, colour etc and how they’re organised in relation to one another. When you start to learn about Flutter you hear the term ‘widget tree’ a lot but actually Flutter has two other trees that work alongside the widget tree to efficiently paint and repaint the UI as needed. The widget tree is something that rebuilds frequently.</p><h4>The Render Tree</h4><p>All of the widgets we have put together eventually form a series of pixels to be displayed on the screen. These groups of pixels actually correspond to objects called <a href="https://api.flutter.dev/flutter/rendering/RenderObject-class.html">RenderObjects</a>. These RenderObjects define the dimensions and position of the visual content and also identify the zones on the screen that may eventually be affected by gestures. It’s all of these RenderObjects combined that form the Render Tree. Below is a description for the <a href="https://flutter.dev/docs/resources/inside-flutter">Flutter docs</a>:</p><blockquote>The <strong>render tree</strong> is a data structure that stores the geometry of the user interface, which is computed during layout and used during painting and hit testing. Most <strong>Flutter</strong> developers do not author <strong>render</strong> objects directly but instead manipulate the <strong>render tree</strong> using widgets.</blockquote><h4>The Element Tree</h4><p>The element tree is like the middleman between the first tree (widget) and the final tree (render). Whenever Flutter encounters a widget that it hasn’t seen before it creates an <a href="https://api.flutter.dev/flutter/widgets/Element-class.html">Element</a> for it.</p><blockquote><em>To </em><strong><em>each</em></strong><em> widget corresponds </em><strong><em>one</em></strong><em> element. Elements are linked to each other and form a tree. Therefore an </em><strong><em>element</em></strong><em> is a reference of something in the tree.</em></blockquote><p>This element holds no actual configuration regarding the widget but rather it’s just like an object stored in memory that points to the widget. The element however does know what type of widget it’s pointing to and is also aware of its position in relation to other elements.</p><p>While the element points to the widget, if that widget is of a type that actually renders something to the screen then the element will also point to its corresponding render object.</p><p>The element tree is what does the comparing to the determine whether the render tree needs to update what’s painted on the screen.</p><h4>Putting it all together</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*95e34pDyXtQlCuI_SXywNQ.png" /><figcaption>Representation of Flutter’s three trees</figcaption></figure><p>When setState is called within a stateful widget, that widget is marked as ‘dirty’ which triggers a call to that widgets build method. This in turn calls all the build methods of all the widgets nested inside that widget unless they’ve been marked as const. Widgets are actually immutable therefore this rebuilding process recreates that section of the tree completely with new instances created to replace the previous ones.</p><p>Each corresponding element looks at the new configuration its pointing to and compares it to the information it currently holds regarding that widget and also its position in relation to its neighbouring elements. If the type and position are the same then it simply updates its reference to point to the newly created widget. Otherwise that part of the element tree and all of its children detach and are rebuilt as each new widget is encountered.</p><p>Note: You can also see the State object sitting outside of the stateful element. That is because it lives independent of the stateful element and persists throughout the life of the widget.</p><p>Second note: In cases of lists you’re able to provide a key to make sure that the widgets are updated correctly.</p><p><a href="https://medium.com/flutter/keys-what-are-they-good-for-13cb51742e7d">Keys! What are they good for?</a></p><h4>A Couple Best Practices</h4><p>There have been a couple of practices that my team have been able to establish early on in relation to these subjects. Not only do these keep the code clean and readable but also helps to improve performance of your app. There are so many more that you can read up on also…</p><h4><strong>Extracting Widgets</strong></h4><p>If you read through the Flutter <a href="https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo">style guide</a> you will see that there is a <a href="https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo">preference for your maximum line length to be just 80 characters</a> 😱 This actually makes a lot of sense after coding in Flutter for more than 10 minutes. The widget tree can get very big, very fast. Extracting widgets into their own classes is a good practice to make the code easier to digest but also it isolates sections giving them their own build methods which can help with performance optimisations also. Limiting the characters to 80 is a constant reminder to extract wherever possible.</p><h4>Declare Const Widgets Where Possible</h4><p>Even though the widget tree rebuilding is just rebuilding configurations rather than the entire UI, it still makes sense to avoid unnecessary rebuilds to save processing power. A great way to do this is by declaring widgets that know their data values at compilation as const widgets. In theory all Stateless widgets can be thought of as const widgets. When prefaced with const in the widget tree, the widget will never have its build method called again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*4k56IhNx_mPXFU_xdTvqxQ.png" /><figcaption>Avoid unnecessary rebuilds by using const widgets when data is known during compilation</figcaption></figure><h4>Conclusion</h4><p>After just a couple of weeks working with Flutter I am excited by what I have learnt and am super pumped to learn more and share more about the journey in the coming months. If you want to dive deeper into some of these topics, check out the links below!</p><ul><li><a href="https://flutter.dev/docs/development/ui/widgets-intro">Introduction to widgets</a></li><li><a href="https://flutter.dev/docs/perf/rendering/best-practices">Performance</a></li><li><a href="https://www.didierboelens.com/2019/09/flutter-internals/">Flutter - Flutter internals</a></li><li><a href="https://medium.com/flutter-community/flutter-best-practices-and-tips-7c2782c9ebb5">Flutter: Best Practices and Tips</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16cc8ed58298" width="1" height="1" alt=""><hr><p><a href="https://medium.com/carsguide-engineering/my-introduction-to-flutter-16cc8ed58298">My Introduction to Flutter</a> was originally published in <a href="https://medium.com/carsguide-engineering">CAMS Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I wanna use regex… but what does it all mean??? ]]></title>
            <link>https://medium.com/carsguide-engineering/i-wanna-use-regex-but-what-does-it-all-mean-a52c52fd441b?source=rss-d52388feefd6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a52c52fd441b</guid>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Spencer Dezart-Smith]]></dc:creator>
            <pubDate>Thu, 11 Jul 2019 02:52:13 GMT</pubDate>
            <atom:updated>2019-07-11T02:52:13.308Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first started programming I really wanted to be good at regex. I was working on a simple little project that involved scraping a website for plant data. This data was like most scraped data… deeply nested and not easily digestible.</p><p>“Why don’t you just use regex to get the data you need?” I remember someone saying.</p><p>Such a simple little sentence.</p><p>That first attempt was successful at making me feel overwhelmed and generally confused. Along with that confusion though I also felt intrigued. Every time I play around with regular expressions, I feel mysterious and powerful kinda like I am saving the world…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/0*LxbbeSCz-y7jN31p" /></figure><p>But generally what ends up happening is…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/0*4DqJeQGFRTIsHBNa.gif" /></figure><p>I have attempted to learn regex numerous times but quite often find myself just googling patterns for different things as opposed to putting them together myself. Well, I decided that it was time to say enough is enough and learn.</p><p>I wanted to put together this piece as a reference for myself and others who may also be interested in learning regex. I am basically just going to outline and explain all of the different elements I learn during this journey.</p><h4>What is regex?</h4><blockquote>A <strong>regular expression</strong>, <strong>regex</strong> or <strong>regexp</strong> (sometimes called a <strong>rational expression</strong>) is a sequence of <a href="https://en.wikipedia.org/wiki/Character_(computing)">characters</a> that define a <em>search </em><a href="https://en.wikipedia.org/wiki/Pattern_matching"><em>pattern</em></a>. Usually such patterns are used by <a href="https://en.wikipedia.org/wiki/String_searching_algorithm">string searching algorithms</a> for “find” or “find and replace” operations on <a href="https://en.wikipedia.org/wiki/String_(computer_science)">strings</a>, or for input validation.</blockquote><p>Regex can be used for many things such as validation, extracting certain parts of a string, cleaning up files and data manipulation… the list actually goes on way longer than that.</p><h3>All the things</h3><p>Regular expressions are written between two forward slashes</p><pre>/regexpatterngoeshere/</pre><h4>Flags or Modifiers</h4><p>There are some flags you can include with your regular expression which are placed after the closing forward slash. Flags denote different requirements.</p><p>//g Global mode: will return all matches is a test string. Without the g flag, the regular expression will return with only the first match.</p><p>//i Insensitive: picks up characters without case considerations</p><p>//m Multiline mode: this is used in relation to anchors <em>(See below). </em>Basically, if you have text that runs on many lines then the anchors will be used for each line when multiline mode is on</p><p>//gmi These flags can be used singularly or together</p><h4>Character literals</h4><p>These are the simplest form of characters. A character is anything that can be typed on your keyboard. You can think of these as input that equals input ie. apples = apples.</p><pre>/true/</pre><p>Will match the first instance of <em>true</em> it finds in a given string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*IgM0YtVEra0kA2eMfQjf0g.png" /></figure><pre>/true/g</pre><p>Will match all the instances of <em>true </em>it finds in a given string because it has the <strong>global </strong>flag attached to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*HmHeLK-nFGBzal9UMm1X2Q.png" /></figure><h4><em>Character class</em></h4><p>A character class is anything written between square brackets [] . You can kind of think of it as a list of characters you can match as opposed to an exact string like we gave in the above example. When we said /true/ we were saying “only match if you find all of these letters in this exact order”. It doesn’t match the letters t r u e at random. Thats what character classes are for.</p><pre>/[abcd]/g</pre><p>The above regex will match all of these letters wherever they occur. They can occur in order or at random.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*ckYXAqL3-bR6_woJNz9qbg.png" /></figure><p>Character classes can be used to provide different options for which letters may appear at a certain point in a string.</p><p>Say you have a bunch of words all ending in ad but you only want to select <em>sad, bad, mad</em>. You would pass the <em>s,b,m</em> as the different acceptable first letters that must be followed by ad .</p><pre>/[sbm]ad/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*Ir1DiouURErIwyJPGGlApQ.png" /></figure><h4>Character Range</h4><p>A range is a set of characters that exist between and inclusive of a lower and upper bound.</p><p>1-10 represents all the numbers that fall between 1 and 10.</p><p>In regex we place ranges inside of a character class just as we did in the above examples. [starting — ending] a range is inclusive of the starting and ending characters</p><p>[a-z] All lowercase characters.</p><p>[e-j] All lowercase characters between and inclusive of <em>e</em> and<em> j.</em></p><p>[A-Z] All uppercase characters.</p><p>[0–9] All numbers.</p><p>You can also combine ranges together like so:</p><p>[a-z0-9] will match all lowercase characters and all numbers.</p><p>[a-zA-Z0-9] will match all lowercase, uppercase characters and numbers.</p><p>Say you have a list of employees and you only want to extract employees one through five.</p><pre>/employee[1-5]/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/158/1*SwM7wJtasI4FyMfHh6sfgw.png" /></figure><p>You can also use ranges for each position of a string.</p><pre>/[A-D][aeiou][b-g]/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*An6CZj85ueqBB6dl4CUr-A.png" /></figure><p>Please note that when it comes to ranges you must always give the lowest value at the starting position and the highest at the ending position. You can think of our characters in terms of their mathematical index. That way you can provide logical ranges as the lower value always needs to reside on the left. If you’re curious about what each chars index is you could run the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nn8FrTQ0UxTzkfYXKyko1Q.png" /></figure><p>Basically A === 65 and a === 97 therefore you could pass [A-a] and that would be valid. It would match all uppercase letters and a lowercase <em>a.</em></p><p>You could also pass [A-z] and you would match all uppercase and lowercase because z === 90 which is greater than the value for <em>A </em>which is<em> 65.</em></p><h4>Negation character</h4><p>Sometimes you are looking to match everything <strong>except </strong>certain characters. This is called negation. The syntax is very similar to the character class with one small difference. The carrot or hat symbol ^ is included as the first symbol inside the [] .</p><p>Say you want to match everything except number.</p><pre>/[^0-9]/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*Ma0C8SyJGFNusgoazyKKYw.png" /></figure><p>Basically you’re saying “Hey give me everything other than the characters I have included within the square brackets”.</p><h4>Meta characters</h4><p>In regex all characters can be described as either a normal character or a meta character. A meta character is basically a character that serves a special purpose. It is important to learn what each of the meta characters are so don’t get tripped up when writing your patterns. Below I will explain all of the meta characters in regex.</p><p><strong>Predefined character classes</strong></p><p>There are some character classes that are super common therefore there are special symbols used to represent them.</p><p>\d match any number</p><p>\D match anything <strong>except</strong> a number</p><p>\w match only word characters [A-z0–9] and underscore _</p><p>\W match only non word characters</p><p>\s match only whitespace (spaces, tabs and newlines [\r\n\t\f\v ])</p><p>\S match all non whitespace</p><p><strong>Anchors</strong></p><p>^ Before start of a string</p><p>$ After the end of a string</p><p>Anchors are actually interesting meta characters because they don’t actually match anything. Rather they set up the positioning of your pattern.</p><pre>/^aaaa/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/1*K_7tALOnQ-sNK8SgYMcCzQ.png" /><figcaption>Matches the first a because it falls directly after the start of the string</figcaption></figure><pre>/\d$/gm</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/120/1*h3xjVywT8c-DVJ9TbvJI8Q.png" /></figure><p>Note that in the above example I am using the multiline mode which is treating each line with a beginning and an end. If we took that flag away we wouldn’t have a match at all because the last character before the $ would actually be the o .</p><p><strong>Wild card</strong></p><p>. Matches everything except line breaks including tabs and spaces.</p><pre>/./g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/154/1*JdZIZZ8Hq00lMNLnLi28Rg.png" /><figcaption>Using the wild card meta character</figcaption></figure><p>The wild card can be used to represent a position in a string eg. /.ee/ would match <em>bee, tee, see</em> etc.</p><p><strong>Word boundary</strong></p><p>\b A word boundary is defined as a word character that is not preceded by a word character or followed by a word character. That sounds confusing right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*PfbdPMo669XzY5kK.gif" /><figcaption>Ahhh excuse me….</figcaption></figure><p>Basically it’s saying if there is a space before and after a string of characters then that would be considered a word boundary. You can kinda think of it as a ‘whole word only’ match.</p><p>Here is an example. I would like to only match the word <em>one </em>but as you can see the pattern <em>one</em> exists within many other words. Thats when a word boundary is useful.</p><pre>/one/gm </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*t2miI6e-7TF1eLd3rd_-Wg.png" /><figcaption>Trying to match the word ‘one’ without a word boundary</figcaption></figure><pre>/\bone\b/gm</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*pWz6a9pK6mpv2Z-9uvkkfA.png" /><figcaption>Thats better!</figcaption></figure><p><strong>Quantifiers</strong></p><p>When these symbols are placed after a group of chars or a single char they represent the ways in which that group or single char can occur.</p><p>? one or none times</p><pre>/colou?r/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/108/1*iafQOXWLjmXQHtqGzsdsOg.png" /><figcaption>Depending on where you come from, colour is spelt differently. Therefore the ‘u’ is optional.</figcaption></figure><p>+ one or more times</p><pre>/happy\d+/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*C1a5R3efm2tFK4SA0xtnVQ.png" /><figcaption>happy which MUST be followed one or more numbers</figcaption></figure><p>* either none or any amount of times</p><pre>/happy\d*/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*0qTCe9SfvpCxvXxWgLr2rw.png" /><figcaption>happy followed by none or some numbers.</figcaption></figure><p>{n} a certain number of times</p><pre>/o{3}/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/1*PGv0SyYHIfdpRVLyFBTSdw.png" /><figcaption>find occurrences of the letter o exactly 3 times</figcaption></figure><p>{min,} a minimum number of times</p><pre>/o{3,}/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*zwjywCpuAPvKAuw7mX5Kog.png" /><figcaption>find occurrences of the letter o a minimum of 3 times</figcaption></figure><p>{min, max} within a minimum and maximum number of times</p><pre>/o{3,6}/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*BXcmNfMUSRBNeVBP_cNZPg.png" /><figcaption>find occurrences of the letter o a minimum of 3 times and maximum of 6 times</figcaption></figure><p><strong>Greedy quantifiers</strong></p><p>It’s important to point out that quantifiers are greedy in nature. Take the example below:</p><pre>/\d{3,6}/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*4II7WFG5EjJZBVSUOMJXrQ.png" /></figure><p>You may look at this and be like WTF. This doesn’t make any sense. What is happening is the greedy quantifier is eating everything up. It works from left to right and counts ahead the min number, checking if it’s a match, then it keeps going until it reaches the max number and then starts again.</p><p>In the above example, the first row 4 occurrences is cool because it falls between out 3 and 6. On the second row it takes 6 because it will always take as many as possible within the range. On the third row it has taken 6, 2 times and then matched 3. It will always keep going until the upper limit is met, then it will start again until it falls outside of the range.</p><p>Another example of a greedy quantifier is:</p><pre>/&lt;.+&gt;/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Ps7fB2taxImgi-7gtdikqQ.png" /><figcaption>Hey I didn’t want all that!</figcaption></figure><p>Let’s say your intention here was to capture only the tags. That regex pattern looks like it should work. Instead its given you a whole lot of stuff. Greeeeedy! Whats happening is it has matched the &lt; and then it basically goes all the way to the end of the string looking for potential &gt; . It gets to the end of the comment and doesn’t see one there so it makes its way backwards, stopping at the first occurrence of &gt; .</p><p><strong>Lazy quantifier</strong></p><p>The secret to solving the greedy problem is a lazy quantifier. By adding a ? directly after the quantifier, you instruct it to only take the minimum. In other words stop at the first match of the quantifier.</p><pre>/&lt;.+?&gt;/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*JaQIClTkOQANWLE4wQnzdg.png" /><figcaption>I am lazy</figcaption></figure><p>There are some performance tradeoffs with using the lazy quantifier. It’s not really a big deal when working with a small string or file but can have quite an impact on a larger file. The way to counter this is to use a negation character instead.</p><pre>/&lt;.[^&gt;]+&gt;/g</pre><p>In this pattern you are saying… go ahead and take everything EXCEPT the &gt; . That way it’s treating the &gt; only as the end of the quantifier. You can test the performance on a regex editor which will show you how many steps it takes to find the matches. Our lazy modifier took 17 steps to find the matches while the negation character took 8.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2DE6TdC1rnFqLvFBipTXgQ.png" /><figcaption>15 steps were taken to achieve the same result with the lazy modifier</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z91iy0wYaAwt76MprjgSrA.png" /><figcaption>only 10 steps taken using the approach</figcaption></figure><p><strong>Escaping meta characters</strong></p><p>When you want to use a meta character in its literal sense then it needs to be escaped. In order to escape a meta character you place a backslash in front of it. This basically negates its special meaning.</p><p>For example, in order to match all of the periods/full stops in a sentence you would place a backslash before the .</p><pre>/\./g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/1*u8zf4iHhW92Wq7D2lsZhpA.png" /><figcaption>Negating the wild card meta character</figcaption></figure><h4>Groups</h4><p>Groups are created by placing a pattern between () . These can be useful by making parts of the pattern reusable, improving readability and giving you the ability to use quantifiers on an entire group. Do note that groups can have a performance impact. If you can create a pattern without a group, go with that.</p><pre>/thanks\s(xo)*/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/212/1*FyiWAdaU_xu1VhdYsHcTQQ.png" /><figcaption>By creating a group (xo) I was able to add a quantifier to the group</figcaption></figure><p><strong>Numbering and Back Referencing</strong></p><p>Numbering is a great was to make groups reusable. Based on the order in which your groups have been assigned, you can use that position number preceded by a \ to reference it again.</p><pre>/thanks\s(xo)*(\slove\salways\s)?\1?/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*JMQPT3pagSuzunukPjMOEQ.png" /><figcaption>using numbering to reference a previously assigned group</figcaption></figure><p>In the above example I am saying to match the thanks followed by a space and any number of the group xo . The last line breaks away from the regular pattern but I still want it. Therefore I make the subsequent niceties optionally but creating another group (\slove\salways\s)? and then by using \1 I am able to reference the first capturing group again.</p><p>Another simple example is matching a date.</p><pre>/(\d{2})-(\1)-(\d{4})/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/136/1*pZ98Pa4TPmIc5qc9-1xoow.png" /></figure><p>There are limitations to this back referencing pattern. Let me show you. Say you have a date pattern that you would like to match the same as above. Your test strings are the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*5fCRaXTazeCkGyfxRqd9XQ.png" /><figcaption>Weird “dates” but just go with me..</figcaption></figure><p>You set up the following regex pattern. This pattern says ‘grab the first two digits followed by a dash.’ Reference the first group, meaning ‘grab the next two digits followed by a dash. The grab the next four digits’. Then we reference the third capturing group and make it optional. So ‘if there is another dash followed by four digits then grab it.</p><pre>/(\d{2})-(\1)-(\d{4})(-\3)?/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*ZZLkqKzcaKizrVAbF2-KIg.png" /><figcaption>Not necessarily what we expected.</figcaption></figure><p>The reason some of these are successful and some aren’t isn’t super clear at first glance. The first date matched fine when the second one didn’t??? This is because of the big back referencing “gotcha”. Because the second set of two digits is referencing the first set, it is expecting it to be an EXACT match. Therefore if the first set of two is 12 then the second set needs to be a 12 also. The same with the four digit reference on line four. It wants the last optional reference to be 2005 not 2008 . Therefore if you don’t expect these values to be exactly the same then don’t use back referencing/numbering.</p><p>Another thing to be mindful of is that capturing groups use resources. If you don’t need to reference them down the line then you can save those extra resources by turning it into a non capturing group by placing a ?: just inside the opening ( eg. (?:blah) .</p><pre>/(\d{2})-(\1)-(?:\d{4})-?(\3)?/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*Njz7WNEeb9kC2cdMBIUR3g.png" /><figcaption>The third group is now non capturing so the \3 reference no longer works.</figcaption></figure><h4>Alternation</h4><p>Alternation basically means the logical or symbol. This is super helpful when there are differences in part of string but the rest is generally the same. It is symbolised by the | or pipe character.</p><pre>/There is a (cat|bat) with a (hat|cat)/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*rYlri9aQIpyz7z3yNMI_dA.png" /></figure><p>Another example is selecting phone numbers with only particular area codes. Say you’re only looking for phone numbers with the area code 02 and 06 :</p><pre>/(02|06)\d*/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/130/1*DxprcM6L_boVwpMu7i4sTQ.png" /></figure><h4>Nesting of alternation inside groups</h4><p>You can next your alternation patterns inside of groups. Here you have an alteration on the outer pattern but also one on each of the inner patterns.</p><pre>/(trueblood\s(s1|s2))|(dexter\s(s1|s2))/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/164/1*gqORMNouBoQPRP5prBF5SQ.png" /></figure><h3>Assertions</h3><p>There are two types of assertions, lookahead assertion and the lookbehind assertion. They are used to match a character or charters that are either preceded by or immediately followed by another character or set of characters. Whatever is listed as the assertion is not part of the match.</p><h4>Positive lookahead assertion</h4><p>The positive lookahead assertion is denoted by the following symbols ?= . In plain english it’s used to say something like “<em>only match a 6 if its followed immediately by a 7”.</em></p><pre>/\w*(?=\ss2)/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/164/1*7OZfj91DPzdQiJr28QPTsw.png" /><figcaption>Match everything that is followed by a space and s2</figcaption></figure><h4>Negative lookahead assertion</h4><p>The negative lookahead assertion is basically the opposite of the positive lookahead. In plain english it’s saying <em>“only match these characters if they are NOT followed by the following assertion”. </em>Let’s write a pattern to match all the important files that do not have a .txt extension.</p><pre>/importantFile(?!\.txt)/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*aAmdo9jVeClJI-4M5uwNrA.png" /><figcaption>No .txt files</figcaption></figure><h4>Positive lookbehind assertion</h4><p>The lookbehind assertion is denoted by the following symbol ?&lt;= and is saying <em>“only match these characters if they are immediately preceded by the following assertion”.</em></p><p>Lets only match the words that are preceded by <em>great</em>:</p><pre>/(?&lt;=great\s)\w*/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*c4UmMmTBlOtQUybbfCMw5w.png" /><figcaption>great things</figcaption></figure><h4>Negative lookbehind assertion</h4><p>As you can probably imagine the negative lookbehind is the opposite of the above. Basically saying <em>“only match this if it’s NOT preceded by the following characters”.</em></p><p>When i was learning regex I was given a great challenge when it comes to this type of assertion. The challenge was, given the following functions names only select the parenthesis following updphone .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/136/1*nNcAdNGnJBoCh_mjBkpX_A.png" /><figcaption>Only select the () that follows updphone</figcaption></figure><p>My initial solution didn’t return the result I was expecting.</p><pre>/((?&lt;!getphone)|(?&lt;!setphone))\(\)/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/138/1*JjxLFM-9SFH5fK8odTDnFQ.png" /><figcaption>Wait, what?</figcaption></figure><p>It turns out the the reason for this result is the alternation | . Because first it reads <em>don’t match the </em>()<em> that follows </em>getphone . Therefore it matches the () after<em> </em>updPhone<em> </em>and<em> </em>setphone<em> .</em> Then is reads <strong><em>or</em></strong><em> don’t match the</em> () <em>that follows </em><em>setphone . </em>Therefore it matches getphone .</p><p>This is a super gotcha. If you remove the | you will yield the correct result.</p><pre>/((?&lt;!getphone)(?&lt;!setphone))\(\)/g</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/142/1*LbAuccLxvxy4U1pN844Bdw.png" /><figcaption>Hooray for overcoming obstacles</figcaption></figure><h3>Conclusion</h3><p>So there you have it folks. You have joined me on this journey to overcome my inability to write my own regex patterns with confidence. It feels really empowering to look at a regex pattern and understand what is going on. This is a complex language to learn but remember practice practice practice… then when the time comes to write your own patterns it won’t seem so hard after all.</p><h3>Practice your skills</h3><p>Below are some lists of sites you can visit to challenge your skills. The best thing you can do with regex is practice.</p><p>RegexPlay: <a href="http://play.inginf.units.it/#/">http://play.inginf.units.it/#/</a></p><p>Regex Crossword: <a href="https://regexcrossword.com/">https://regexcrossword.com/</a></p><p>FreeCodeCamp: <a href="https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/">https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a52c52fd441b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/carsguide-engineering/i-wanna-use-regex-but-what-does-it-all-mean-a52c52fd441b">I wanna use regex… but what does it all mean??? 😫</a> was originally published in <a href="https://medium.com/carsguide-engineering">CAMS Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>