figbert.com-website

[ACTIVE] the website and home of figbert on the clearnet
git clone git://git.figbert.com/figbert.com-website.git
Log | Files | Refs | README | LICENSE

my-first-regex.md (2106B)


      1 +++
      2 title = "My First RegEx"
      3 date = 2021-01-21
      4 +++
      5 
      6 `(?<=\.\/IssuerIcons\/).*(?=\.png)`
      7 <!-- more -->
      8 
      9 ## What it does
     10 
     11 Let's break it apart piece by piece. It's meant to return the filename
     12 from a given path in a specific format. When given
     13 `./IssuerIcons/Example.png`, the script should return `Example`.
     14 
     15 The first section is a positive lookbehind: `(?<=\.\/IssuerIcons\/)`.
     16 This asserts that the selected portion must by preceeded by
     17 `./IssuerIcons/`.
     18 
     19 The section section is a pattern match: `.*`. This matches all
     20 characters, excluding newlines, of any length.
     21 
     22 The third and final section is a positive lookforward: `(?=\.png)`.
     23 This asserts that the selected portion must be followed by `.png`.
     24 
     25 ## Background
     26 
     27 Yesterday, I wrote my first regular expression. I've always regarded
     28 regex as an arcane art of true shell wizards – and for the most part,
     29 I still do. Now though, I've gotten a glimpse of their world.
     30 
     31 I wrote this expression for [Tofu][tofu], an open-source 2FA app for
     32 iOS. I had been thinking of making more "issuer icons" for the app
     33 when I noticed that the icons were all designed in one Sketch file.
     34 This could become a problem if two people were to edit the file at
     35 one time, so I suggested a [solution][issue].
     36 
     37 The author got back to me with another idea: ditch Sketch altogether,
     38 and replace it with a shell script that generates icons from a
     39 directory of `png`s. I [wrote it][pr]. I've since refined it, with
     40 help from [ThinkChaos][tc], to make it independent of any preceding
     41 directories using `sed`: `sed -E 's:.+/(.+)\.png:\1:'`.
     42 
     43 It was a really fun experience – I've never written bash before! It
     44 reminded me a lot of Drew DeVault's post [Become shell literate][sl].
     45 I work primarily from my terminal, and am slowly working my way up to
     46 true mastery – I'm nowhere near close, but this was a step in the right
     47 direction.
     48 
     49 [tofu]: https://www.tofuauth.com/
     50 [issue]: https://github.com/calleerlandsson/Tofu/issues/52
     51 [pr]: https://github.com/calleerlandsson/Tofu/pull/60
     52 [sl]: https://drewdevault.com/2020/12/12/Shell-literacy.html
     53 [tc]: https://github.com/ThinkChaos