• some_guy@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 hours ago

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

  • aMockTie@piefed.world
    link
    fedilink
    English
    arrow-up
    5
    ·
    21 hours ago

    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.

  • BiscuityCat@lemmy.world
    link
    fedilink
    arrow-up
    13
    ·
    1 day ago

    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:

    (?:^-\[[^[]+\]$| ?\[.+?\] ?)
    
    • lime!@feddit.nu
      link
      fedilink
      arrow-up
      1
      ·
      20 hours ago

      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
        link
        fedilink
        arrow-up
        1
        ·
        3 hours ago

        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
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          30 minutes ago

          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.

  • ignirtoq@feddit.online
    link
    fedilink
    English
    arrow-up
    21
    ·
    2 days ago

    *sees rfc822 in a regular expression*

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

    *checks*

    Yep.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      10
      ·
      1 day ago

      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.

      • naticus@lemmy.world
        link
        fedilink
        English
        arrow-up
        4
        ·
        1 day ago

        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.