6 comments

  • My coworker used to teach non-technical people how to code. Students always found regex hard to understand.<p>Let&#x27;s be honest, regex can be hard to understand even for technical people. (At least it&#x27;s hard for me.)<p>So coworker built this to help anyone understand arbitrary regex commands.<p>AI disclosure: this was built using Agent Duel, a local coding agent arena system (<a href="https:&#x2F;&#x2F;github.com&#x2F;bottomless&#x2F;agent-duel" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bottomless&#x2F;agent-duel</a>). Promotion disclosure: our company is building agent duel. To test it and find bugs we are building passion projects through the system. I nonetheless thought HN would find this interesting.
    • crazygringo55 minutes ago
      Regex is <i>conceptually</i> incredibly easy to understand.<p>But regex is also <i>syntactically</i> designed by Satan himself.<p>Most programming languages use commands that are semantically understandable, like &quot;for&quot; and &quot;let&quot; and &quot;return&quot; and mostly symbols we already know like &quot;+&quot; and &quot;=&quot;. They use newlines and indentation for organization. And they put multiple commands on multiple lines and you can add comments and everything.<p>Regex is exclusively punctuation, with its own idiosyncratic meanings, all compressed into a single line, zero organization, zero comments, and impossible to remember what each symbol does if you don&#x27;t use it daily. Not to mention the nightmare of escaping regexes when expressed as strings, <i>on top of</i> escaping regex symbols within the regex itself.<p>I swear it&#x27;s high time we come up with a regex replacement that looks like normal Python code or HTML or something. So instead of:<p><pre><code> ^(\+\d{1,3}-?)?\d{3}-\d{3}-\d{4}$ </code></pre> We would write something like:<p><pre><code> &lt;start&gt; &lt;optional&gt; # Optional country code &quot;+&quot; &lt;digit&gt;[1-3] &quot;-&quot; &lt;&#x2F;optional&gt; &lt;digit&gt;[3] # Area code &quot;-&quot; &lt;digit&gt;[3] &quot;-&quot; &lt;digit&gt;[4] &lt;end&gt; </code></pre> Same thing. But one is completely inscrutable to someone who hasn&#x27;t learned regex, while with the other you can guess pretty easily how it works...
      • wongarsu38 minutes ago
        If you want readability you can write<p><pre><code> ^(?P&lt;country_code&gt;\+[0-9]{1,3}-?)?[0-9]{3}-[0-9]{3}-[0-9]{4}$ </code></pre> Most regex engines even have a verbose mode (typically the x flag) where this is a valid regex:<p><pre><code> ^ ( \+ [0-9]{1,3} -? )? # optional country code [0-9]{3} - [0-9]{3} - [0-9]{4} $ </code></pre> I am not sure if everyone writes unreadable regex because the reputation of regex makes it seem pointless to try, or if being witty one-liners is a critical part of the value proposition of regex
      • mejutoco31 minutes ago
        Regex in xml? Now you have 2 problems. And the comments are not xml comments even.<p>Ultimately it is a dsl. Should equations in math also use full words and comments?<p>No, they should not. Maybe a nice syntax for grammars like raku is a better approach.
      • bazoom4236 minutes ago
        Most regex dialects allow whitespace and comments, so that already gets you halfway there.
      • sgarland35 minutes ago
        You can enable extended mode for most engines, which allows you to add white space and comments.<p>Personally, I like regexes, in the same way that I like shell string manipulation: it looks utterly inscrutable, and performs magic. If I write one in code meant for others, I usually use extended mode with each line commented.
      • ogogmad25 minutes ago
        I think some languages do have a library-style version of regex. But the function names aren&#x27;t consistent between the different libraries. Maybe then we need to put on special goggles to make them look the same.
    • atoav3 hours ago
      I am mostly an autodidact and learned bits regex on the fly when I needed it and I was always baffled why people have auch a hard time with it.<p>The problem IMO is that a lot of the traditional teaching material starts with the complexity (and impressive possibilities) of regex, when the best way to start learning it is probably just thinking about it as an extension to simple text matching.<p>Getting to know regex fully is in fact complicated, getting know enough regex to filter&#x2F;search&#x2F;replace 90% of everything you will always need is not too complicated.
      • ogogmad3 hours ago
        Autodidact here. Beyond just the five core quantifiers<p><pre><code> . | ? * + </code></pre> regex only has capture groups<p><pre><code> () </code></pre> and lazy variants of the core quantifiers &quot;?&quot;, &quot;*&quot; and &quot;+&quot;, which are helpfully called:<p><pre><code> ?? *? +? </code></pre> The rest is just shorthands.<p>So while I think it&#x27;s a worthwhile suggestion not to show people the whole thing the first time round, I doubt that the whole thing is really that complicated.
        • StilesCrisis1 hour ago
          Regex has many more features than this, e.g. backreferences. I agree that it&#x27;s easy to learn enough regex to be dangerous with that subset, but that&#x27;s not everything regex offers.
          • teiferer30 minutes ago
            There are hundred different flavors of what &quot;regex&quot; actually means. The basics are often the same but the extended features differ.<p>The foundation is in theoretical CS with deterministic and nondeterministic finite automata (DFAs and NFAs), and I found it easiest to teach regexes using DFAs since you can then just draw circles and arrows. Regex notation is just a different notation. It&#x27;s a bit likeif you know piano and then learn another instrument, your brain can always refer back to the piano and it&#x27;s all much easier.<p>Backreferences are not really &quot;controversial&quot; but just an extension which does not map to regular languages (those recognized by finite automata). That is, they make regexes &quot;non-regular&quot;, so they are more a hack. Regex engines that want to support them need a fundamentally different approach, that&#x27;s why some don&#x27;t support them and in principle will never be able to.
            • ogogmad18 minutes ago
              They are controversial because supporting them raises matching time to O(2^n) in the worst case - which is very dangerous - while not supporting them enables matching time to be O(n) even in the worst case. And a user might not be aware of that danger.<p>Also, I think it&#x27;s mistaken to mention NFAs and DFAs the way you just did, like there&#x27;s no deeper reason. Regexes (or NFAs, or DFAs) simply classify exactly the properties of strings which can be checked in constant space.<p>Also, no regex implementation actually uses DFAs. NFAs yes, but DFAs no. The point of NFAs is that they&#x27;re easy to execute in constant space and linear time, while also making concatenation and Kleene star easy to implement. It seems much harder to implement concatenation and Kleene star using other constant-space automata - like DFAs - which is what actually forced people to invent NFAs.<p>I really don&#x27;t like this approach which is pedantic about NFAs and DFAs without any deeper motivation at all. The motivation is out there but almost never given.
          • ogogmad1 hour ago
            Backreferencing inside of a regex (which is a way to find <i>repeat</i> occurrences of a substring) is a very controversial feature that some regex engines intentionally omit, while most others support it only grudgingly. Supporting backreferences opens up a big can of worms: Most regex engines will fall back to a slow, poorly optimised algorithm if they ever encounter backreferences.<p>Also, it&#x27;s important for people <i>not</i> to confuse backreferences with capture groups. They look a bit similar, but capture groups are actually fine.<p>In fact, backreferencing is such a footgun that I don&#x27;t see it as a possible regex feature. It&#x27;s a PCRE feature - perhaps even the core distinguishing feature. While all regexes are PCREs, not all PCREs are regexes. It&#x27;s simply not possible to create a consistently performant implementation of PCRE.
    • ogogmad3 hours ago
      Really cool. Do you teach people the mechanics, like what the app shows? Because I wonder if that actually makes it <i>harder</i>. I&#x27;m wondering about where the hard bits are, given that it all boils down to just:<p><pre><code> &quot;|&quot;, which means &quot;or&quot; &quot;.&quot;, which means &quot;any symbol&quot; &quot;?&quot;, which means &quot;at most once&quot;, &quot;*&quot;, which means &quot;however many times&quot; &quot;+&quot;, which means &quot;at least once&quot; </code></pre> Maybe encountering it next to PEG scrambles your brain.
  • monster_truck4 hours ago
    This is actually pretty satisfying, I wish it had sound. Maybe a dramatic follow cam for the balls
  • fwlr2 hours ago
    Seems the input text box is limited to 301 characters.
  • Sarkie1 hour ago
    Now I have 3 problems
  • deakmisra3 hours ago
    [flagged]
  • rbaRBA1 hour ago
    [flagged]