okr765
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
VegOwOtenks@lemmy.world to Programmer Humor@lemmy.mlEnglish · 2 years ago

Regex flavors

lemmy.world

external-link
message-square
30
link
fedilink
402
external-link

Regex flavors

lemmy.world

VegOwOtenks@lemmy.world to Programmer Humor@lemmy.mlEnglish · 2 years ago
message-square
30
link
fedilink
alert-triangle
You must log in or # to comment.
  • dactylotheca@suppo.fideleted by creator
    link
    fedilink
    English
    arrow-up
    79
    arrow-down
    3
    ·
    edit-2
    3 months ago

    deleted by creator

    • qaz@lemmy.world
      link
      fedilink
      arrow-up
      26
      ·
      2 years ago

      Regex really isn’t that bad when using named capture groups.

      • dactylotheca@suppo.fideleted by creator
        link
        fedilink
        English
        arrow-up
        17
        ·
        edit-2
        3 months ago

        deleted by creator

        • frezik@midwest.social
          link
          fedilink
          arrow-up
          12
          ·
          2 years ago

          It’s entirely possible to parse HTML in PCRE. You shouldn’t, but it is possible. The language stopped being strictly regular a long time ago and is entirely capable of doing it.

          https://stackoverflow.com/a/4234491/830741

          • dactylotheca@suppo.fideleted by creator
            link
            fedilink
            English
            arrow-up
            7
            ·
            edit-2
            3 months ago

            deleted by creator

        • FooBarrington@lemmy.world
          link
          fedilink
          arrow-up
          5
          ·
          2 years ago

          I’ve once written a JS decompiler (de-bundler?) using ~150 regex for step-wise transformations. Worked surprisingly well!

          • Azzk1kr@feddit.nl
            link
            fedilink
            English
            arrow-up
            4
            ·
            2 years ago

            What eldritch beast was summoned as a result?

            • FooBarrington@lemmy.world
              link
              fedilink
              arrow-up
              2
              ·
              2 years ago

              Well… No new ones, at least? Though it was around that time that I started hearing whispers in the night… “You can use WASM to ship Client-Side PHP”

        • bleistift2@sopuli.xyz
          link
          fedilink
          English
          arrow-up
          3
          ·
          2 years ago

          it is literally impossible to use regular expressions to parse languages that aren’t regular

          It’s impossible to parse the whole syntax tree, but that doesn’t mean you can’t get the subset you’re interested in.

    • TryingSomethingNew@lemmy.worlddeleted by creator
      link
      fedilink
      arrow-up
      3
      ·
      2 years ago

      Jwz’s 2nd law!

    • MashedTech@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      2 years ago

      I learned Regex once and now it just works. Only problem for me is using MacOS so the Regex flavors aren’t consistent. But once I sort that, it’s smooth sailing.

  • AnarchistArtificer@slrpnk.net
    link
    fedilink
    English
    arrow-up
    47
    ·
    2 years ago

    Regex feels distinctly eldritch to me. Like, a lot of computing knowledge feels like magic, but regex feels like the kind of magic you get by consorting with dark forces

    • TunaCowboy@lemmy.worlddeleted by creator
      link
      fedilink
      arrow-up
      43
      ·
      2 years ago

      regex feels like the kind of magic you get by consorting with dark forces

      AKA reading the manual.

      • Tangent5280@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        2 years ago

        Im a good christian boy thats why I refuse to read the manual

      • Cysioland@lemmygrad.ml
        link
        fedilink
        arrow-up
        3
        ·
        2 years ago

        Or studying computer science and learning about finite state machines

  • whodatdair@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    45
    ·
    2 years ago

    Blasphemy, that’s not regex that’s just fancy grep

    • VegOwOtenks@lemmy.worldOP
      link
      fedilink
      arrow-up
      10
      ·
      2 years ago

      I don’t actually know whether POSIX grep would support named groups :o

      • qaz@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        Don’t have you have to use the -P flag?

        • OmnislashIsACloudApp@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          2 years ago

          which is just perl mode

          • qaz@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            2 years ago

            Yes, but perl mode has more features.

    • kubica@fedia.io
      link
      fedilink
      arrow-up
      3
      ·
      2 years ago

      I don’t fully disagree but you are walking on a fine line…

    • PotatoesFall@discuss.tchncs.de
      link
      fedilink
      arrow-up
      2
      ·
      2 years ago

      any idea what the re in grep stands for?

  • ☆ Yσɠƚԋσʂ ☆@lemmy.ml
    link
    fedilink
    arrow-up
    21
    arrow-down
    1
    ·
    2 years ago

    I really like this approach for doing non trivial regex https://github.com/VerbalExpressions

    const tester = VerEx()
        .startOfLine()
        .then('http')
        .maybe('s')
        .then('://')
        .maybe('www.')
        .anythingBut(' ')
        .endOfLine();
    
    • frezik@midwest.social
      link
      fedilink
      arrow-up
      6
      arrow-down
      4
      ·
      edit-2
      2 years ago

      I don’t. It may look less like line noise, but it doesn’t unravel the underlying complexity of what it does. It’s just wordier without being helpful.

      https://www.wumpus-cave.net/post/2022/06/2022-06-06-how-to-write-regexes-that-are-almost-readable/index.html

      Edit: also, these alternative syntaxes tend to make some easy cases easy, but they have no idea what to do with more complicated cases. Try making nested capture groups with these, for instance. It gets messy fast.

      • JoeyJoeJoeJr@lemmy.ml
        link
        fedilink
        arrow-up
        7
        ·
        2 years ago

        it doesn’t unravel the underlying complexity of what it does… these alternative syntaxes tend to make some easy cases easy, but they have no idea what to do with more complicated cases

        This can be said of any higher-level language, or API. There is always a cost to abstraction. Binary -> Assembly -> C -> Python. As you go up that chain, many things get easier, but some things become impossible. You always have the option to drop down, though, and these regex tools are no different. Software development, sysops, devops, etc are full of compromises like this.

        • ☆ Yσɠƚԋσʂ ☆@lemmy.ml
          link
          fedilink
          arrow-up
          3
          arrow-down
          1
          ·
          2 years ago

          Exactly, at the end of the day it’s about using the right tool for the job. Code that’s clear and declarative is easier to maintain, so it makes sense to default to it, but nothing stops you from using low level constructs if you really need to.

  • itsathursday@lemmy.world
    link
    fedilink
    arrow-up
    16
    ·
    2 years ago

    Named groups are nice but can I please define a group more than once because maybe I want to group my data and consolidate values in a logical way without you complaining I have already used a group previously. I know I did, I’m the one telling you, now capture it twice!

  • jaybone@lemmy.world
    link
    fedilink
    arrow-up
    8
    ·
    2 years ago

    Can you actually name capture groups, or this means how you can refer to them by number?

    • VegOwOtenks@lemmy.worldOP
      link
      fedilink
      arrow-up
      25
      ·
      2 years ago

      You can use backreferences \1 \2 etc. but you can also give them names explicitly.
      it looks like this: (?<name>inner-regex)
      Some flavors support it, kotlins doesn’t apparently.

      • jaybone@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        2 years ago

        TIL thanks!

    • mormund@feddit.org
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 years ago

      In modern languages you can name them with labels as well yes. Not sure about the syntax right now. Something like (?label:…) I think

      • qaz@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        2 years ago

        It’s (?<NAME>...) and those are the named capture groups referred to in the post.

  • neidu2@feddit.nl
    link
    fedilink
    arrow-up
    7
    ·
    2 years ago

    I don’t see the problem. But that’s probably because my goto-language is perl.

Programmer Humor@lemmy.ml

programmerhumor@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmerhumor@lemmy.ml

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 59 users / day
  • 69 users / week
  • 1.56K users / month
  • 4.02K users / 6 months
  • 1 local subscriber
  • 44.2K subscribers
  • 1.57K Posts
  • 20.9K Comments
  • Modlog
  • mods:
  • AgreeableLandscape@lemmy.mldeleted by creator
  • cat_programmer@lemmy.ml
  • BE: 0.19.18
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org