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