this post was submitted on 27 Sep 2026
516 points (98.1% liked)

memes

23084 readers
2406 users here now

Community rules

1. Be civilNo trolling, bigotry or other insulting / annoying behaviour

2. No politicsThis is non-politics community. For political memes please go to !politicalmemes@lemmy.world

3. No recent repostsCheck for reposts when posting a meme, you can only repost after 1 month

4. No botsNo bots without the express approval of the mods or the admins

5. No Spam/Ads/AI SlopNo advertisements or spam. This is an instance rule and the only way to live. We also consider AI slop to be spam in this community and is subject to removal.

A collection of some classic Lemmy memes for your enjoyment

Sister communities

founded 3 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] BiscuityCat@lemmy.world 13 points 3 weeks ago (4 children)

I never understood why are people scared of regular expressions. I love them and they can be quite usefull.

Here is an example of one regex I wrote recently - it is used in replace:

(?:^-\[[^[]+\]$| ?\[.+?\] ?)
[–] Axolotl_cpp@feddit.it 6 points 3 weeks ago* (last edited 3 weeks ago)
[–] heartSagan5@lemmy.zip 1 points 3 weeks ago (1 children)

I could use a use case. RegEx are pretty "what does it mean" without what it's targeted on.

[–] BiscuityCat@lemmy.world 1 points 3 weeks ago

I have mentioned it in the comments below, but to explain it simply, I just wanted to remove all the bracketed text + a dash if there is no other text.

[–] lime@feddit.nu 1 points 3 weeks ago (1 children)

that feels overcomplicated. you're always matching on square brackets anyway, so why make it a conditional? and one case not allowing spaces seems inconsistent...

[–] BiscuityCat@lemmy.world 1 points 3 weeks ago (1 children)

It may feel like it, but I needed to handle all the edge cases. But if you think it can be simplified, I'm willing to learn.

[–] lime@feddit.nu 1 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

without knowing the format of the input data or what flags you're using it's obviously a bit harder to reason about, but since you're doing a replace i assume you don't care about the content of the bracketed text, you just want to remove it. the way i'm reading yours is

(?: # don't store this group
  # either
    ^  # start of line
    -\[[^[]+\]  # a -, a [ followed by at least one non-[, then a ] (passing one is fine)
    $  # end of line 
 | # or
  ?  # maybe one space
 \[.+?\]  # a [ followed by at least one character, then a ] (don't pass any)
  ? # maybe one space
)

since the two branches are almost identical, you can probably compact it into -?\s*\[.+?\]\s*. maybe a dash, then maybe some space, then angle brackets around something, then maybe more space.

[–] BiscuityCat@lemmy.world 4 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

You are right. It is just removal of the bracketed text. The input is multi-line string, so only flag used was the multi-line mode.

These are string lines I have encountered and how I wanted to change them:

1. "[...]"               -> ""
2. "-[...]"              -> ""
3. "-[...] text"         -> "-text"
4. "-text [...]"         -> "-text"
5. "[...] text [...]"    -> "text"
6. "-[...] text [...]"   -> "-text"

The main point is that I needed to remove the dash only if there is no other text on the line.

I have tried the regex you are suggesting at the beginning, but it failed at points (3) and (6).

EDIT: It just occurred to me that the first line of my comment sounds like stupid AI. 🤦

[–] lime@feddit.nu 1 points 3 weeks ago

aaah i see. then a conditional makes sense if you need to one-shot it. personally i would have run it through twice but i know there are systems that don't allow that.

[–] Funkt4st1c@lemmy.world 1 points 3 weeks ago

I started using regex because i need to rename a lot of my pirated media from "this_episode_uploaded_byXxXSuperEl33tSqwaaadXxX_MLP_s04_Episode7_bats.mkv"

and linux deals with it very nicely. Much better than manual, anyways