💾 Archived View for ayushnix.com › microgemlog › 2021-12-05-regexes-are-awesome.gmi captured on 2024-03-21 at 15:18:09. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
Let's say you download a useful bash script that uses bubblewrap to sandbox electron bloat like Signal. The script is great but, unfortunately, it uses "function blah { }" to define functions instead of "blah() { }". There are 19 such "functions" in this script. You could manually change all of them to the latter format. Or, you could use a regex in vim and change it in a few seconds.
:%s:^function \([[:alpha:]_]\+\) {:\1() {:g
"[[:alpha:]_]" matches an alphabet and the _ character. The "\+" ensures to match multiple letters of this pattern. The ^ at the beginning ensures that the line starts with function.
We've surrounded the pattern we wrote with \( and \) which is called "grouping". All we did is "capture" that pattern because we'll need it later when we make the substitution we want. We didn't capture function and "{" because we want to remove them.
All that's left is to make the substitution with \1, our captured pattern, and () {.
Awesome.
---
Created: 2021-12-05