this post was submitted on 04 Oct 2025
183 points (100.0% liked)

Programmer Humor

38798 readers
276 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS
 
top 11 comments
sorted by: hot top controversial new old
[–] TootSweet@lemmy.world 18 points 2 weeks ago (1 children)

Regexes in bash commands are my nemesis.

[–] NotSteve_@piefed.ca 12 points 2 weeks ago (1 children)

Regexes ~~in bash commands~~ are my nemesis.

[–] levzzz@lemmy.world 4 points 2 weeks ago (1 children)
[–] eager_eagle@lemmy.world 2 points 2 weeks ago (1 children)
[–] meekah@lemmy.world 3 points 2 weeks ago

scott pilgrim?

[–] Ephera@lemmy.ml 13 points 2 weeks ago* (last edited 2 weeks ago) (2 children)

This meme brought to you by me trying to pass a regex from Nix into a TOML, which is certainly not the worst backslash orgy I've seen, but tragic in its own right. Both Nix and TOML have a way to specify raw strings, which do not need escaping. But Nix uses a normal string when passing into TOML, so I do have to escape anyways. 🫠

My regex also contains a double-quote, which was just guesswork as to how many backslashes I need there.

[–] balsoft@lemmy.ml 3 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

But Nix uses a normal string when passing into TOML, so I do have to escape anyways

What do you mean by that? You are always able to just use the '' strings instead of the " strings, they are just different syntax for the same underlying type. Or are you just using lib.generators.toINI without any arguments? Maybe try something like this:

toTomlEscapeBackslashes = toINI {
  mkKeyValue = mkKeyValueDefault { mkValueString = x: lib.escape [ ''\'' ] (toString x); } "=";
}

This will escape the values, like this:

nix-repl> :print toTomlEscapeBackslashes { my.regex = ''foo\nbar''; }
[my]
regex=foo\\nbar
[–] Ephera@lemmy.ml 3 points 2 weeks ago

I figured, I'd involuntarily sign up for counter suggestions by posting this. 😅

Using lib.escape is a good idea, thanks.

But yeah, basically I want to configure Alacritty and I'm using the respective home-manager module.
Even more specifically, I want to pass stuff, including a regex, into the settings parameter, which more-or-less just takes the Nix expression that I shove in and then outputs it as TOML on disk.

As for how I would've liked this to work:

But the TOML is templated with "double-quotes", so I do need to escape the regex after all.

I did just try to understand how the Alacritty module does the templating and found this gem:

# TODO: why is this needed? Is there a better way to retain escape sequences?
"substituteInPlace $out --replace-quiet '\\\\' '\\'"

Source

So, that doesn't fill me with a whole lot of confidence. 🙃

But yeah, it's working now without me having to write a whole bunch of backslashes, so that's good enough in my book.

[–] chgxvjh@hexbear.net 3 points 1 week ago* (last edited 1 week ago)

My personal best is backslash in a json string in an env variable passed to bash in a docker container, in a batch file.

[–] TimeSquirrel@kbin.melroy.org 12 points 2 weeks ago (1 children)

Learning C: Random asterisks go

[–] vext01@lemmy.sdf.org 6 points 2 weeks ago

But sometimes ampersands also!