<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Scotty</title>
    <description>The latest articles on Forem by Scotty (@kanga_bru).</description>
    <link>https://forem.com/kanga_bru</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F419940%2Ff97a7001-2cff-48c8-81af-1dc5c48b9c7f.jpg</url>
      <title>Forem: Scotty</title>
      <link>https://forem.com/kanga_bru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kanga_bru"/>
    <language>en</language>
    <item>
      <title>Powerful regex for the practical dev #2</title>
      <dc:creator>Scotty</dc:creator>
      <pubDate>Sat, 12 Sep 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/kanga_bru/powerful-regex-for-the-practical-dev-2-i94</link>
      <guid>https://forem.com/kanga_bru/powerful-regex-for-the-practical-dev-2-i94</guid>
      <description>&lt;p&gt;As a developer you’re constantly working with large amounts of text like source code, logs, and data files. Often you need to extract, replace, or manipulate that text and regex can help you.&lt;/p&gt;

&lt;p&gt;Today I present variants of a real case study where I personally used regex. These examples build upon my last post so check that out too.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/kanga_bru" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OkwJky3d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--U0ynbS_B--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/419940/f97a7001-2cff-48c8-81af-1dc5c48b9c7f.jpg" alt="kanga_bru image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/kanga_bru/powerful-regex-for-the-practical-dev-4pj6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Powerful regex for the practical dev&lt;/h2&gt;
      &lt;h3&gt;Scotty ・ Sep  1 ・ 6 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#regex&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#search&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;





&lt;h2&gt;
  
  
  Case study example
&lt;/h2&gt;

&lt;p&gt;This is a snippet of &lt;a href="https://gist.github.com/kangabru/2d20fe8089223fe91c34169cd71ba5f2"&gt;this csv file&lt;/a&gt; which you can also &lt;a href="https://regexr.com/5c19f"&gt;play with here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reddit_id,colorblind_comment,score,title,url,created
...
8cwcbu,False,101457,"Cause of Death - Reality vs. Google vs. Media [OC]",https://i.imgur.com/GtIzEok.gif,1523970172
8bzdr8,False,99626,"Gaze and foot placement when walking over rough terrain (article link in comments) [OC]",https://v.redd.it/h0f0m4v5nor01,1523628194
fpga3f,False,99488,"[OC] To show just how insane this week's unemployment numbers are, I animated initial unemployment insurance claims from 1967 until now. These numbers are just astonishing.",https://i.redd.it/tch0t0is32p41.gif,1585245693
i2vx78,True,98638,"The environmental impact of Beyond Meat and a beef patty [OC]",https://i.redd.it/jskjkodg3se51.png,1596456703
fxoxti,False,98067,"Coronavirus Deaths vs Other Epidemics From Day of First Death (Since 2000) [OC]",https://v.redd.it/yemjrb1p9rr41,1586422082
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;(Top 100 posts from r/dataisbeautiful on Reddit where 'colorblind' is mentioned in the comments)&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Text extraction: Extract urls from data
&lt;/h2&gt;

&lt;p&gt;There are a ton of things you could extract here but I’ll go with this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Extract image urls from posts with colorblind comments&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You &lt;em&gt;could&lt;/em&gt; use excel or write a script here but this is why I would use regex:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quick and easily done from your IDE&lt;/li&gt;
&lt;li&gt;Not limited to CSVs - this could be source code which &lt;em&gt;isn’t&lt;/em&gt; so easy via excel/scripts&lt;/li&gt;
&lt;li&gt;Took me under a minute to do (with prior regex knowledge)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #1:&lt;/strong&gt; Select all lines where the colorblind column is &lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.*,True,.*&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J3A8h83J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-01.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J3A8h83J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-01.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;True&lt;/code&gt; match the word ‘True’&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;,&lt;/code&gt; match commas (so we don’t match ‘True’ in the title text)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; match everything before and after&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #2:&lt;/strong&gt; Limit to lines that have an image url&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.*,True,.*http.*\.(png|jpg).*&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v2JNmuWX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-02.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v2JNmuWX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-02.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;http.*&lt;/code&gt; match start of url (not necessary but needed for the next step)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\.(png|jpg)&lt;/code&gt; match &lt;code&gt;.png&lt;/code&gt; or &lt;code&gt;.jpg&lt;/code&gt; extensions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http.*\.(png|jpg)&lt;/code&gt; combine to match urls with an image extension&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*,True,.*&lt;/code&gt; + &lt;code&gt;http.*\.(png|jpg)&lt;/code&gt; + &lt;code&gt;.*&lt;/code&gt; combine step 1 regex, the image url, and everything after. Now it matches lines with colorblind comments AND image urls&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #3:&lt;/strong&gt; Match just the url:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(?&amp;lt;=.*,True,.*,)http.*\.(png|jpg)(?=.*)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wng20m1P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-03.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wng20m1P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-03.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets break up our regex so far into parts:&lt;/p&gt;

&lt;p&gt;| &lt;code&gt;part_1&lt;/code&gt; | Before the url | &lt;code&gt;.*,True,.*&lt;/code&gt; |&lt;br&gt;
| &lt;code&gt;part_2&lt;/code&gt; | The url | &lt;code&gt;http.*\.(png|jpg)&lt;/code&gt; |&lt;br&gt;
| &lt;code&gt;part_3&lt;/code&gt; | After the url | &lt;code&gt;.*&lt;/code&gt; |&lt;/p&gt;

&lt;p&gt;We want to isolate &lt;code&gt;part_2&lt;/code&gt; so must exclude &lt;code&gt;part_1&lt;/code&gt; and &lt;code&gt;part_3&lt;/code&gt; like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;= ... ) ...&lt;/code&gt; exclude stuff &lt;em&gt;before&lt;/em&gt; a match&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;... (?= ... )&lt;/code&gt; exclude stuff &lt;em&gt;after&lt;/em&gt; a match&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;= part_1 )&lt;/code&gt; + &lt;code&gt;part_2&lt;/code&gt; + &lt;code&gt;(?= part_3 )&lt;/code&gt; exclude &lt;code&gt;part_1&lt;/code&gt; and &lt;code&gt;part_3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;=.*,True,.*)http.*\.(png|jpg)(?=.*)&lt;/code&gt; substitute in our actual regex&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #4:&lt;/strong&gt; Extract the text&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KLjPh6e1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-04.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KLjPh6e1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-04.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A modern IDE should now let you select your matches. In VS Code you can select and copy your matches like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Alt + Enter&lt;/code&gt; to select matches&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ctrl + c&lt;/code&gt; to copy matches&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ctrl + v&lt;/code&gt; to paste matches (in a separate file)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Urls successfully extracted! You could take this further by running this over your &lt;em&gt;entire&lt;/em&gt; repo using global search (&lt;code&gt;Alt + Shift + f&lt;/code&gt;). Show me an excel script that does that!&lt;/p&gt;




&lt;h2&gt;
  
  
  Text replace: Reformat url structure
&lt;/h2&gt;

&lt;p&gt;Sometimes we don’t want to extract text but rather update it. For this example we’ll update all image urls in the file like so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;from this: &lt;code&gt;https://i.redd.it/jskjkodg3se51.png&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;to this: &lt;code&gt;https://i.redd.it/png/jskjkodg3se51&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #1:&lt;/strong&gt; Match image urls (with ID match)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http.*/.*\.(png|jpg)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nw_kvHhf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-05.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nw_kvHhf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-05.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This the url regex we made before but with an image ID match that we’ll use in the next step&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http.*&lt;/code&gt; match start of url and everything after&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/.*&lt;/code&gt; match forward slash and everything after (matches the image ID)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\.(png|jpg)&lt;/code&gt; match &lt;code&gt;.png&lt;/code&gt; or &lt;code&gt;.jpg&lt;/code&gt; extensions&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #2:&lt;/strong&gt; Create regex groups&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(http.*)/(.*)\.(png|jpg)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l8uTsrmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-06.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l8uTsrmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-06.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Everything wrapped with &lt;code&gt;()&lt;/code&gt; creates a regex group which we can reference in our replace command.&lt;/p&gt;

&lt;p&gt;Given the url &lt;code&gt;https://i.redd.it/jskjkodg3se51.png&lt;/code&gt; we must create groups for the following parts to transform them as desired:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;https://i.redd.it/&lt;/code&gt; the domain part&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jskjkodg3se51&lt;/code&gt; the image ID part&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;png&lt;/code&gt; the image extension part&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s group those parts of our regex:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(http.*)&lt;/code&gt; groups the domain part&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/(.*)&lt;/code&gt; groups the image ID part (note the slash is excluded)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\.(png|jpg)&lt;/code&gt; groups the image extension part (note this was already grouped and that &lt;code&gt;.&lt;/code&gt; is excluded)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Step #3:&lt;/strong&gt; Replace&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(http.*)/(.*)\.(png|jpg)&lt;/code&gt; + &lt;code&gt;$1/$3/$2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tLc0BVRi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-07.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tLc0BVRi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-07.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mssx_rUH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-08.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mssx_rUH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kangabru.xyz/images/posts/2020-09-12/regex/regex-08.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the replace UI (&lt;code&gt;Ctrl + h&lt;/code&gt; in most IDEs)&lt;/p&gt;

&lt;p&gt;We can now reference groups in replace commands as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$0&lt;/code&gt; is the entire match&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, &lt;code&gt;$3&lt;/code&gt; is the first, second, third group etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our search and replace commands will therefore look like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search: &lt;code&gt;(http.*)/(.*)\.(png|jpg)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Replace: &lt;code&gt;$1/$3/$2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That regex replace will change urls like so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;https://i.redd.it/jskjkodg3se51.png&lt;/code&gt; -&amp;gt; &lt;code&gt;https://i.redd.it/png/jskjkodg3se51&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;https://i.redd.it/mkdikcce8gh31.jpg&lt;/code&gt; -&amp;gt; &lt;code&gt;https://i.redd.it/jpg/mkdikcce8gh31&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nice huh? You can also do this globally via &lt;code&gt;Alt + shift + h&lt;/code&gt; to search and replace over your whole repo. Very useful.&lt;/p&gt;




&lt;p&gt;Powerful stuff eh? Once you learn how to build up a regex like this (hint: practice makes perfect) you can do this kind of stuff in seconds. Searching, extracting, and transforming text will become second nature.&lt;/p&gt;

&lt;p&gt;Well that’s it for now! I’ve got 2 more sections of this case study ready to go but this article was getting long. So next week we’ll learn how to use multi-cursors to augment regex even further.&lt;/p&gt;

&lt;p&gt;It’s seriously powerful stuff! &lt;/p&gt;

&lt;p&gt;Found this useful? &lt;a href="https://twitter.com/intent/tweet?url=https%3A%2F%2Fdev.to%2Fkanga_bru%2Fpowerful-regex-for-the-practical-dev-2-i94&amp;amp;via=kanga_bru&amp;amp;text=Wow%20regex%20is%20powerful%21%20If%20you%27re%20a%20dev%20and%20NOT%20using%20regex%20then%20you%27re%20missing%20out.&amp;amp;hashtags=regex"&gt;Tweet about it&lt;/a&gt; or &lt;a href="https://twitter.com/kanga_bru"&gt;follow me&lt;/a&gt; on Twitter. Until next time 👋&lt;/p&gt;

</description>
      <category>regex</category>
      <category>search</category>
    </item>
    <item>
      <title>Powerful regex for the practical dev</title>
      <dc:creator>Scotty</dc:creator>
      <pubDate>Tue, 01 Sep 2020 18:07:08 +0000</pubDate>
      <link>https://forem.com/kanga_bru/powerful-regex-for-the-practical-dev-4pj6</link>
      <guid>https://forem.com/kanga_bru/powerful-regex-for-the-practical-dev-4pj6</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Regex is search on steroids. This post demonstrates how to create powerful searches by example.&lt;/p&gt;

&lt;p&gt;Examples start basic and build up to more complex expressions. They are designed for devs who search via their IDE.&lt;/p&gt;

&lt;p&gt;We'll use this text throughout (&lt;a href="https://regexr.com/5b6mg"&gt;demo&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alabama       (AL)  Birmingham  (Dec 14, 1819)  4,903,185
Hawaii*       (HI)  Honolulu    (Aug 21, 1959)  1,415,872
Michigan      (MI)  Detroit     (Jan 26, 1837)  9,986,857
North Dakota  (ND)  Fargo       (Nov  2, 1889)    762,062
Wyoming*      (WY)  Cheyenne    (Jul 10, 1890)    578,759
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;(state, id, largest city, founding date, population)&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  How to use this guide
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Browse quickly by skimming the picture/title&lt;/strong&gt;. There are many examples.&lt;/li&gt;
&lt;li&gt;If you're a beginner start at the top&lt;/li&gt;
&lt;li&gt;Find more advanced/interesting examples further down&lt;/li&gt;
&lt;li&gt;Examples next to each other are related&lt;/li&gt;
&lt;li&gt;Experiment by testing the examples yourself&lt;/li&gt;
&lt;li&gt;Find a cheat sheet at the bottom&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://regexr.com/5b6mg"&gt;Follow along here&lt;/a&gt; or via your IDE:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open a modern IDE (I use VSCode)&lt;/li&gt;
&lt;li&gt;Paste the example&lt;/li&gt;
&lt;li&gt;Open search (&lt;code&gt;ctrl + f&lt;/code&gt; or &lt;code&gt;cmd + f&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Enable regex (usually a &lt;code&gt;.*&lt;/code&gt; icon)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Series
&lt;/h2&gt;

&lt;p&gt;This is the first part in a short series I'll be making. Following guides will cover real case studies and more useful concepts like regex replace.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kangabru.xyz/"&gt;Follow the newsletter&lt;/a&gt; or hit me up on &lt;a href="https://twitter.com/kanga_bru"&gt;Twitter&lt;/a&gt; for more.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basic matches
&lt;/h2&gt;

&lt;p&gt;Letters &lt;code&gt;[a-zA-Z]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GZJLbp6H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ozaeb1lex9mjskgnksut.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GZJLbp6H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ozaeb1lex9mjskgnksut.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]&lt;/code&gt; lowercase letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[A-Z]&lt;/code&gt; uppercase letters&lt;/li&gt;
&lt;li&gt;Casing only matters when the 'match case' (&lt;code&gt;Aa&lt;/code&gt;) option is enabled in VSCode&lt;/li&gt;
&lt;li&gt;102 matches found because it's matching &lt;code&gt;a-z&lt;/code&gt; &lt;em&gt;characters&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Words &lt;code&gt;[a-zA-Z]+&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--acLoHPfg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ft0ulfarkc3kv4g75jk9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--acLoHPfg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ft0ulfarkc3kv4g75jk9.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-zA-Z]&lt;/code&gt; letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; repeats the match for consecutive characters&lt;/li&gt;
&lt;li&gt;21 matches found because it's matching &lt;code&gt;a-z&lt;/code&gt; &lt;em&gt;words&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p class="text-center"&gt;Specific words &lt;code class="language-plaintext highlighter-rouge"&gt;(Jan|Jul|Dec)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZDog5pkE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6o4qdjbzizfm2i1h6e2r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZDog5pkE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6o4qdjbzizfm2i1h6e2r.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(Jan|Jul|Dec)&lt;/code&gt; matches &lt;code&gt;Jan&lt;/code&gt;, &lt;code&gt;Jul&lt;/code&gt;, or &lt;code&gt;Dec&lt;/code&gt; specifically&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;2 numbers &lt;code&gt;[0-9]{2}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q4jwPA1K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3prunjaan3pfx5v1wet8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q4jwPA1K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3prunjaan3pfx5v1wet8.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; numbers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{2}&lt;/code&gt; match twice&lt;/li&gt;
&lt;li&gt;Note long numbers contain multiple matches&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;4 numbers &lt;code&gt;[0-9]{4}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e41UkBpi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7khmkd5kmiwi07dtjyr3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e41UkBpi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7khmkd5kmiwi07dtjyr3.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; numbers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{4}&lt;/code&gt; match 4 times&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;2-3 letters &lt;code&gt;[a-z]{2,3}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3TkPeul3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e1kj9zugty2u30owwe0w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3TkPeul3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e1kj9zugty2u30owwe0w.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]&lt;/code&gt; letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{2,3}&lt;/code&gt; match between 2 to 3 times (inclusive)&lt;/li&gt;
&lt;li&gt;Note long words contain multiple matches&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;6+ letters &lt;code&gt;[a-z]{6,}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4c_dYql1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v7vbr36kjufqqyu7vxmg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4c_dYql1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v7vbr36kjufqqyu7vxmg.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]&lt;/code&gt; letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{6,}&lt;/code&gt; match 6 or more times (inclusive)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;3 letters/numbers &lt;code&gt;\w{3}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wMQUxPk9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p88imgo9l3it4cluttkp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wMQUxPk9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p88imgo9l3it4cluttkp.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\w&lt;/code&gt; letters and numbers (see special chars)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{3}&lt;/code&gt; match 3 times&lt;/li&gt;
&lt;li&gt;Note long words contain multiple matches&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;3 whole letters/numbers &lt;code&gt;\b\w{3}\b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NkWi6coe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8h8cbf7l7ucztwd73s3o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NkWi6coe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8h8cbf7l7ucztwd73s3o.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\w{3}&lt;/code&gt; match 3 letters and numbers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\b&lt;/code&gt; word boundaries (see special chars)&lt;/li&gt;
&lt;li&gt;Note long words &lt;em&gt;don't&lt;/em&gt; contain multiple matches&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;3 whole letter words &lt;code&gt;\b[a-z]{3}\b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ec6TO8qs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3dsnlwh25pq7ny2dhb7q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ec6TO8qs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3dsnlwh25pq7ny2dhb7q.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]{3}&lt;/code&gt; match 3 letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\b&lt;/code&gt; word boundaries&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Two words &lt;code&gt;[a-zA-Z]+\s[a-zA-Z]+&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OulVFCgz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qcz08nprdfn43i365q3z.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OulVFCgz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qcz08nprdfn43i365q3z.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Looks scarier than it is. The form is &lt;code&gt;word space word&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-zA-Z]+&lt;/code&gt; word&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\s&lt;/code&gt; space (see special chars)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;One or two words &lt;code&gt;[a-zA-Z]+(\s[a-zA-Z]+)?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wdeiyL_B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/djr88iuyksjtuhtojz92.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wdeiyL_B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/djr88iuyksjtuhtojz92.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Looks scarier than it is. The form is &lt;code&gt;word (space word)?&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-zA-Z]+&lt;/code&gt; word&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\s&lt;/code&gt; space&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;( ... )?&lt;/code&gt; optional&lt;/li&gt;
&lt;li&gt;Note &lt;code&gt;North Dakota&lt;/code&gt; is considered one match now&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Wildcards
&lt;/h2&gt;

&lt;p&gt;Everything in brackets (greedy) &lt;code&gt;\(.*\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v6aSykGk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qbaqi3p1mnq4d4h85rou.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v6aSykGk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qbaqi3p1mnq4d4h85rou.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\(&lt;/code&gt; and &lt;code&gt;\)&lt;/code&gt; match brackets (see special chars)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; greedy wildcard&lt;/li&gt;
&lt;li&gt;This greedy wildcard will match up to the &lt;em&gt;last&lt;/em&gt; &lt;code&gt;)&lt;/code&gt; bracket&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Everything in brackets (non-greedy) &lt;code&gt;\(.*?\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g14huyEF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xhz3o8m26ugj5ecldxkn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g14huyEF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xhz3o8m26ugj5ecldxkn.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\(&lt;/code&gt; and &lt;code&gt;\)&lt;/code&gt; match brackets&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*?&lt;/code&gt; non-greedy wildcard&lt;/li&gt;
&lt;li&gt;This non-greedy wildcard will match up to the &lt;em&gt;first&lt;/em&gt; &lt;code&gt;)&lt;/code&gt; bracket&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Lines with the &lt;code&gt;*&lt;/code&gt; character &lt;code&gt;^.*\*.*$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DUX-T6AO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wl9c4nz8j70pygu5uola.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DUX-T6AO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wl9c4nz8j70pygu5uola.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; match the start/end of the line (&lt;a href="https://dev-to-uploads.s3.amazonaws.com/i/huwr2ibbqytyfk44iaex.jpg"&gt;optional&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\*&lt;/code&gt; the star &lt;code&gt;*&lt;/code&gt; character (see special chars)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Lines without the &lt;code&gt;*&lt;/code&gt; character &lt;code&gt;^[^\*]+$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bqEQsiyR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rg3lqo84wfo8or6u8luc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bqEQsiyR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rg3lqo84wfo8or6u8luc.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; match the start/end of the line&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^ ... ]&lt;/code&gt; matches anything &lt;em&gt;not&lt;/em&gt; in the brackets

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\*&lt;/code&gt; the star &lt;code&gt;*&lt;/code&gt; character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^\*]&lt;/code&gt; matches anything not a &lt;code&gt;*&lt;/code&gt; character&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; repeats the match for consecutive characters&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;All lines with the &lt;code&gt;e&lt;/code&gt; character &lt;code&gt;^.*[e].*$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GClCf5i1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/brfw7ji7wsujrkf66qaz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GClCf5i1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/brfw7ji7wsujrkf66qaz.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; match the start/end of the line&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[e]&lt;/code&gt; the letter &lt;code&gt;e&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;All lines without the &lt;code&gt;e&lt;/code&gt; character &lt;code&gt;^[^e]+$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ml8Nm5sJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tf01rpxfe2eqb7hhqpfr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ml8Nm5sJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tf01rpxfe2eqb7hhqpfr.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; match the start/end of the line&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^ ... ]&lt;/code&gt; matches anything &lt;em&gt;not&lt;/em&gt; in the brackets

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[^e]&lt;/code&gt; matches anything not an &lt;code&gt;e&lt;/code&gt; character&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; repeats the match for consecutive characters&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Brackets starting with certain words &lt;code&gt;\((Jan|Jul|Dec).*\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hyYdc-Kr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ghbskubagcdeevez6xj0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hyYdc-Kr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ghbskubagcdeevez6xj0.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\(&lt;/code&gt; and &lt;code&gt;\)&lt;/code&gt; match brackets&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(Jan|Jul|Dec)&lt;/code&gt; matches &lt;code&gt;Jan&lt;/code&gt;, &lt;code&gt;Jul&lt;/code&gt;, or &lt;code&gt;Dec&lt;/code&gt; words&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; wildcard&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Mixed matches
&lt;/h2&gt;

&lt;p&gt;The short date in brackets &lt;code&gt;[a-z]{3}\s+[0-9]+&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cWKpBlua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1aqmkq25pa82i8miqo9a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cWKpBlua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1aqmkq25pa82i8miqo9a.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]{3}&lt;/code&gt; 3 letters exactly&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\s+&lt;/code&gt; one or more spaces&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0-9]+&lt;/code&gt; one or more numbers&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The date in brackets &lt;code&gt;[a-z]{3}\s+[0-9]+,\s[0-9]+&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0q7M9HS3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e1ovfbv0riatloo0lxjo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0q7M9HS3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e1ovfbv0riatloo0lxjo.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Looks scarier than it is. The form is &lt;code&gt;word number, number&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]{3}&lt;/code&gt; 3 letters exactly&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\s+&lt;/code&gt; one or more spaces&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;,&lt;/code&gt; comma&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0-9]+&lt;/code&gt; one or more numbers&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Words with &lt;code&gt;m&lt;/code&gt; (in the middle) &lt;code&gt;[a-z]+[m][a-z]+&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--liY9SaAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bk3mz1uwxy2lg2qu03cs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--liY9SaAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bk3mz1uwxy2lg2qu03cs.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]+&lt;/code&gt; one or more letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[m]&lt;/code&gt; the letter &lt;code&gt;m&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Note this doesn't match &lt;code&gt;Michigan&lt;/code&gt; because &lt;code&gt;m&lt;/code&gt; is at the start of the word&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Words with &lt;code&gt;m&lt;/code&gt; (anywhere) &lt;code&gt;([a-z]+)?[m]([a-z]+)?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y9wnNMiO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8xtpyjpxsmjyc2a473qg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y9wnNMiO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8xtpyjpxsmjyc2a473qg.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Looks scarier than it is. The form is &lt;code&gt;(word)? m (word)?&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;( ... )?&lt;/code&gt; optional

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[a-z]+&lt;/code&gt; a word&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;([a-z]+)?&lt;/code&gt; an optional word&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[m]&lt;/code&gt; the letter &lt;code&gt;m&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Note &lt;code&gt;m&lt;/code&gt; can be anywhere in the word so &lt;code&gt;Michigan&lt;/code&gt; is matched now&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Exclusive matches
&lt;/h2&gt;

&lt;p&gt;Match expressions but exclude them from the result. Officially known as 'look arounds'.&lt;/p&gt;

&lt;p&gt;Word in brackets (inclusive) &lt;code&gt;\([a-z]+\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3oiICvZH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a344gbytajopttj6w07e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3oiICvZH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a344gbytajopttj6w07e.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note the word is matched &lt;em&gt;with&lt;/em&gt; the brackets&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\(&lt;/code&gt; and &lt;code&gt;\)&lt;/code&gt; match brackets&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]+&lt;/code&gt; a word&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Word in brackets (exclusive) &lt;code&gt;(?&amp;lt;=\()[a-z]+(?=\))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EPER9tWG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h5jk5apu49laldb80i0g.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EPER9tWG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h5jk5apu49laldb80i0g.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note the word is matched &lt;em&gt;without&lt;/em&gt; the brackets&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]+&lt;/code&gt; a word&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;= ... )&lt;/code&gt; starts a match but excludes it from the result

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\(&lt;/code&gt; the bracket &lt;code&gt;(&lt;/code&gt; character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;=\()&lt;/code&gt; matches from bracket &lt;code&gt;(&lt;/code&gt; without including it&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?=  ... )&lt;/code&gt; ends a match but excludes it from the result

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\)&lt;/code&gt; the bracket &lt;code&gt;)&lt;/code&gt; character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?=\))&lt;/code&gt; matches up to bracket &lt;code&gt;)&lt;/code&gt; without including it&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Everything in brackets (exclusive) &lt;code&gt;(?&amp;lt;=\().*?(?=\))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g2EkBOGO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bt5c09ac2hy35d3x8q9y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g2EkBOGO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bt5c09ac2hy35d3x8q9y.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;=\()&lt;/code&gt; matches from bracket &lt;code&gt;(&lt;/code&gt; without including it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*?&lt;/code&gt; non-greedy wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?=\))&lt;/code&gt; matches up to bracket &lt;code&gt;)&lt;/code&gt; without including it&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Everything in brackets on lines with &lt;code&gt;*&lt;/code&gt;  (exclusive)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(?&amp;lt;=\*.*\().*?(?=\))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7_r86wlW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z4nuov7yvenlggukj9ly.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7_r86wlW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z4nuov7yvenlggukj9ly.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;= ... )&lt;/code&gt; starts a match but excludes it

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\*&lt;/code&gt; the star &lt;code&gt;*&lt;/code&gt; character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\(&lt;/code&gt; the bracket &lt;code&gt;(&lt;/code&gt; character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?&amp;lt;=\*.*\()&lt;/code&gt; wildcard from &lt;code&gt;*&lt;/code&gt; to &lt;code&gt;(&lt;/code&gt; without including them&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*?&lt;/code&gt; non-greedy wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?=))&lt;/code&gt; matches up to &lt;code&gt;)&lt;/code&gt; without including it&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Everything up to &lt;code&gt;*&lt;/code&gt; (exclusive) &lt;code&gt;^.*(?=\*)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rXN5IUb9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kkbgd9s1zk3cdhsi0lop.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rXN5IUb9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kkbgd9s1zk3cdhsi0lop.jpg" alt="Regex example in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; start of a line&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.*&lt;/code&gt; wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(?=\*)&lt;/code&gt; matches up to &lt;code&gt;*&lt;/code&gt; without including it&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;. ^ $ * + ? ( ) [ { \ |&lt;/code&gt; reserved characters&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Escape with &lt;code&gt;\&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(abc)&lt;/code&gt; matches &lt;code&gt;abc&lt;/code&gt; (in a regex group)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\(abc\)&lt;/code&gt; matches &lt;code&gt;(abc)&lt;/code&gt; (with brackets)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;[a-zA-Z]&lt;/code&gt; letters (case-sensitive)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[0-9]&lt;/code&gt; or &lt;code&gt;\d&lt;/code&gt; match numbers&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[a-c1-3#]&lt;/code&gt; matches characters &lt;code&gt;a b c 1 2 3 #&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.*&lt;/code&gt; greedy wildcard. &lt;code&gt;.*?&lt;/code&gt; non-greedy wildcard.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;^&lt;/code&gt; start of line. &lt;code&gt;$&lt;/code&gt; end of line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\s&lt;/code&gt; space. &lt;code&gt;\t&lt;/code&gt; tab. &lt;code&gt;\n&lt;/code&gt; new line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\w&lt;/code&gt; letters and numbers. &lt;code&gt;\W&lt;/code&gt; &lt;em&gt;not&lt;/em&gt; letters and numbers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\b&lt;/code&gt; word break. &lt;code&gt;\B&lt;/code&gt; &lt;em&gt;not&lt;/em&gt; word break.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;+&lt;/code&gt; repeat matches&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{3}&lt;/code&gt; repeat match exactly thrice&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{1,3}&lt;/code&gt; repeat match 1, 2, or 3 times&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{3,}&lt;/code&gt; repeat match 3+ times&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[^ ... ]&lt;/code&gt; match all but given characters&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(?&amp;lt;= ... )&lt;/code&gt; start match with given characters and exclude them (look behind)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(?= ... )&lt;/code&gt; end match with given characters and exclude them (look ahead)&lt;/p&gt;

</description>
      <category>regex</category>
      <category>search</category>
    </item>
  </channel>
</rss>
