Expressions

all 10 comments

sorted by: hot top controversial new old
[–] 27 points 1 month ago
[–] 22 points 1 month ago
[–] 15 points 1 month ago (2 children)

none of the meat of the regex shown, 0/10

  • source
  • hideshow 2 child comments
  • [–] 11 points 1 month ago* (1 child)

    Why waste time doing any matching if wantarray == false? Just do:

    if(!wantarray) { return 1; }

    Before any matching happens, or if you only want one return statement set the value of a variable in if(!wantarray)/elsif(regex) and return that variable at the end.

  • source
  • hideshow 1 child comment
  • [–] 10 points 1 month ago

    That Perl. You never know if return doesn't have some kind of hidden behavior that uses the local variables...

    Well, you could read the perldoc page on it. But you can't just assume it.

  • source
  • parent
  • [–] 4 points 1 month ago

    At least the cat is happy again now after getting adopted

    I'll forever be sad trying to understand the regex I've written myself...

  • source
  • [–] 4 points 1 month ago (1 child)

    Doggy is not smiling but trying to lose heat. Doggy is the real regular expression

  • source
  • hideshow 1 child comment
  • 31 comments in c/memes@lemmy.world view that post
    [–] 22 points 1 month ago (6 children)

    *sees rfc822 in a regular expression*

    I bet that's the email specification. That's probably an email address parser.

    *checks*

    Yep.

  • source
  • hideshow 6 child comments
  • [–] 11 points 1 month ago (5 children)

    Perhaps worth adding that verifying an e-mail address via regex is difficult and not actually that useful.

    Difficult, because a lot of e-mail addresses are syntactically valid. For example, a@b is valid. As is a@1.2.3.4. As is "spaces are cool 😎"@b (with the quotes).

    And it's not that useful, because you need to send out an e-mail either way, if you want to know that it's the correct address and the user has access to it. A regex can't catch a typo in the actual words, for example.

    If you want a basic sanity check to catch when the user types their name into the e-mail field, then you can require that it contains an @ and, if you want, that there's at least one symbol before and after.
    If you go beyond that, you either need a massive regex or you're going to lock out users with unusual e-mail addresses.

  • source
  • parent
  • hideshow 5 child comments
  • [–] 5 points 1 month ago (4 children)

    Yep, these email address validation tools are the bane of my existence. I use a non-typical TLD and it sometimes gets flagged as invalid because it's not .com, .net, or .org.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 12 points 1 month ago (9 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:

    (?:^-\[[^[]+\]$| ?\[.+?\] ?)
    
  • source
  • hideshow 9 child 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
  • load more comments (1 reply)
    [–] 40 points 1 month ago (5 children)

    That third one is also sad

  • source
  • hideshow 5 child comments
  • [–] 21 points 1 month ago (3 children)
  • [–] 5 points 1 month ago* (2 children)

    Must be bad at regular expressions. I generally get far more than one extra problem when using them.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 8 points 1 month ago

    Everyone loves perl though! /s

    img

  • source
  • [–] 7 points 1 month ago (1 child)

    Why waste time doing any matching if wantarray == false? Just do:

    if(!wantarray) { return 1; }

    Before any matching happens, or if you only want one return statement set the value of a variable in if(!wantarray)/elsif(regex) and return that variable at the end.

  • source
  • hideshow 1 child comment
  • load more comments (1 reply)
    [–] 3 points 1 month ago (1 child)

    Perl being the first language I started learning, this makes me happy. Oh shit, press the button for the dog!

  • source
  • hideshow 1 child comment
  • [–] 2 points 1 month ago*

    Panting can also be a sign of stress with dogs

  • source
  • [–] 2 points 1 month ago

    Practice safe hex as they say...

  • source
  • [–] 2 points 1 month ago

    Top notch shitpost. Extra credit for perl!

  • source