you are viewing a single comment's thread
view the rest of the comments
[–] 1 point 1 month ago (4 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...

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 1 month ago (3 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.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 1 point 1 month ago* (last edited 1 month ago) (2 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.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 1 month ago* (last edited 1 month ago) (1 child)

    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. 🤦

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 1 month 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.

  • source
  • parent