19 comments

  • mananaysiempre3 hours ago
    Worth mentioning (haven’t checked if the paper talks about this) that while the industry mostly forgot about derivatives and extended REs (i.e. REs with intersection and negation), academia did not. Unfortunately, there have been some pretty discouraging results: the DFA for an extended RE (including a lazy DFA implemented using derivatives, as here) is worst-case <i>doubly</i> exponential in the length of the expression[1], not just exponential as for normal REs. So there is a potential reason not to support intersections in one’s RE matcher, even if they are enticingly easy to implement in terms of derivatives (and even if I personally like to see experimentation in this direction).<p>[1] <a href="https:&#x2F;&#x2F;www.sciencedirect.com&#x2F;science&#x2F;article&#x2F;pii&#x2F;S0304397510002537" rel="nofollow">https:&#x2F;&#x2F;www.sciencedirect.com&#x2F;science&#x2F;article&#x2F;pii&#x2F;S030439751...</a>
    • someplaceguy1 hour ago
      &gt; the DFA for an extended RE (including a lazy DFA implemented using derivatives, as here) is worst-case doubly exponential in the length of the expression<p>The authors seem to claim linear complexity:<p>&gt; the result is RE#, the first general-purpose regex engine to support intersection and complement with linear-time guarantees, and also the overall fastest regex engine on a large set of benchmarks
      • ieviev1 hour ago
        We refer to this in the paper as well,<p>The standard way to do intersection &#x2F; complementation of regexes with NFAs requires determinization, which causes a huge blowup, whereas for us this is the cost of a derivative.<p>It is true that we cannot avoid enormous DFA sizes, a simple case would be (.*a.*)&amp;(.*b.*)&amp;(.*c.*)&amp;(.*d.*)... which has 2^4 states and every intersection adds +1 to the exponent.<p>How we get around this in the real world is that we create at most one state per input character, so even if the full DFA size is 1 million, you need an input that is at least 1 million characters long to reach it.<p>The real argument to complexity is how expensive can the cost of taking a lazy derivative get? The first time you use the engine with a unique input and states, it is not linear - the worst case is creating a new state for each character. The second time the same (or similar) input is used these states are already created and it is linear. So as said in the article it is a bit foggy - Lazy DFAs are not linear but appear as such for practical cases
        • btown27 minutes ago
          &gt; The second time the same (or similar) input is used these states are already created and it is linear.<p>Does this imply that the DFA for a regex, as an internal cache, is mutable and persisted between inputs? Could this lead to subtle denial-of-service attacks, where inputs are chosen by an attacker to steadily increase the cached complexity - are there eviction techniques to guard against this? And how might this work in a multi-threaded environment?
      • mananaysiempre1 hour ago
        These claims are compatible. For instance, lex, re2c, ragel, etc. are exponential in the length of the automaton description, but the resulting lexers work in linear time[1] in the length of the string. Here the situation is even better, because the DFA is constructed lazily, at most one state per input character, so to observe its full enormity relative to the size of the needle you need an equally enormous haystack. “One state per input character” somewhat understates things, because producing each state requires a non-constant[2] amount of work, and storing the new derivative’s syntax tree a non-constant amount of space; but with hash-consing and memoization it’s not bad. Either way, derivative-based matching with a lazy DFA takes something like O(needle + f(needle) × haystack) time where I’m guessing f(n) has to be O(n log n) at least (to bring large ORs into normal form by sorting) but in practice is closer to constant. Space consumption is less of an issue because if at any point your cache of derivatives (= DFA) gets too bloated you can flush it and restart from scratch.<p>[1] Kind of, unless you hit ambiguities that need to be resolved with the maximal munch rule; anyways that’s irrelevant to a single-RE matcher.<p>[2] In particular, introductions to Brzozowski’s approach usually omit—but his original paper does mention—that you need to do some degree of syntax-tree simplification for the derivatives to stay bounded in size (thus finite in number) and the matcher to stay linear in the haystack.
  • noelwelsh4 hours ago
    I love regular expression derivatives. One neat thing about regular expression derivatives is they are continuation-passing style for regular expressions. The derivative is &quot;what to do next&quot; after seeing a character, which is the continuation of the re. It&#x27;s a nice conceptual connection if you&#x27;re into programming language theory.<p>Low-key hate the lack of capitalization on the blog, which made me stumble over every sentence start. Great blog post a bit marred by unnecessary divergence from standard written English.
    • u_sama3 hours ago
      in what is it different ?
      • murkt2 hours ago
        Starts of sentences are not capitalized, which makes it a bit harder to read. English language prescribes capitalization after a period.
  • balakk5 hours ago
    Finally, an article about humans programming some computers. Thank you!
  • agnishom5 hours ago
    The author mentions that they found Mamouras et al. (POPL 2024), but not the associated implementation. While the Rust implementation is not public, a Haskell implementation can be found here: <a href="https:&#x2F;&#x2F;github.com&#x2F;Agnishom&#x2F;lregex" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Agnishom&#x2F;lregex</a>
  • cognisent21 minutes ago
    One thing I don&#x27;t understand is what does _* mean? It seems like the paper refers to .* (which I understand) and _* (which I don&#x27;t) in sometimes the same context? Normally _* would mean &quot;an underscore zero or more times&quot;.
    • shmolyneaux10 minutes ago
      That&#x27;s noted further down the page:<p>- `_*` = any string
  • systems22 minutes ago
    I am a bit worried about the state of F# thought,<p>Don Syme seem to no longer be acting as the project lead, and I didn&#x27;t hear of any successor<p>Compared to most actively developed languages F# look very stale currently
  • gbacon5 hours ago
    That’s beautiful work. Check out other examples in the interactive web app:<p><a href="https:&#x2F;&#x2F;ieviev.github.io&#x2F;resharp-webapp&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ieviev.github.io&#x2F;resharp-webapp&#x2F;</a><p>Back in the Usenet days, questions came up all the time about matching substrings that do <i>not</i> contain whatever. It’s technically possible without an explicit NOT operator because regular languages are closed under complement — along with union, intersection, Kleene star, etc. — but a bear to get right by hand for even simple cases.<p>Unbounded lookarounds without performance penalty at search time are an exciting feature too.
  • klibertp2 hours ago
    If you claim it&#x27;s the fastest, how does it compare to one-more-re-nightmare?<p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;telekons&#x2F;one-more-re-nightmare" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;telekons&#x2F;one-more-re-nightmare</a><p>- <a href="https:&#x2F;&#x2F;applied-langua.ge&#x2F;posts&#x2F;omrn-compiler.html" rel="nofollow">https:&#x2F;&#x2F;applied-langua.ge&#x2F;posts&#x2F;omrn-compiler.html</a><p>OMRN is a regex compiler that leverages Common Lisp&#x27;s compiler to produce optimized assembly to match the given regex. It&#x27;s incredibly fast. It does omit some features to achieve that, though.
    • mananaysiempre2 hours ago
      As a potential user (not the author), what jumps out at me about the two is:<p>OMRN: No lookaround, eager compilation, can output first match<p>RE#: No submatches, lazy compilation, must accumulate all matches<p>Both lookaround and submatch extraction are hard problems, but for practical purposes the lack of lazy compilation feels like it would be the most consequential, as it essentially disqualifies the engine from potentially adversarial REs (or I guess not with the state limit, but then it’s questionable if it actually counts as a full RE engine in such an application).
  • sieep3 hours ago
    Cool stuff. Reminds me of the content you used to see on here all the time before AI took over
    • andix1 hour ago
      I&#x27;m sometimes wondering if the AI content here really starts trending organically, or if it is somehow pushed by AI companies.<p>Not necessarily with bots, just posting a few links in a company Slack with the request for everyone to upvote it from their personal account could be enough.
  • sourcegrift6 hours ago
    I&#x27;ve had nothing but great experience with F#. If it wasn&#x27;t associated with Microsoft, it&#x27;d be more popular than haskell
    • raincole6 hours ago
      I think if it weren&#x27;t a &#x27;first class&#x27; member of .NET ecosystem[0], no one would know F#. After all Haskell and Ocaml already exist.<p>[0]: my very charitable take, as MS obviously cares C# much much more than F#.
      • pjmlp5 hours ago
        Management has always behaved as if they repent having added F# to VS 2010, at least it hasn&#x27;t yet suffered the same stagnation as VB, even C++&#x2F;CLI was updated to C++20 (minus modules).<p>In any case, those of us that don&#x27;t have issues with .NET, or Java (also cool to hate these days), get to play with F# and Scala, and feel no need to be amazed with Rust&#x27;s type system inherited from ML languages.<p>It is yet another &quot;Rust but with GC&quot; that every couple of months pops up in some forums.
      • Copyrightest4 hours ago
        [dead]
    • dude2507116 hours ago
      <i>&gt; ...it&#x27;d be more popular than haskell</i><p><a href="https:&#x2F;&#x2F;steve-yegge.blogspot.com&#x2F;2010&#x2F;12&#x2F;haskell-researchers-announce-discovery.html" rel="nofollow">https:&#x2F;&#x2F;steve-yegge.blogspot.com&#x2F;2010&#x2F;12&#x2F;haskell-researchers...</a>
      • sourcegrift5 hours ago
        &gt; But they all just skip the press releases and go straight to the not using it part<p>Lol
      • brabel4 hours ago
        Did they ever get the full extra person who gives a shit?
    • lynx976 hours ago
      You realize that Microsoft Research employed Simon for many many years?
  • masfuerte4 hours ago
    This is very impressive.<p>&gt; how does RE# find the leftmost-longest match efficiently? remember the bidirectional scanning we mentioned earlier - run the DFA right to left to find all possible match starts, then run a reversed DFA left to right to find the ends. the leftmost start paired with the rightmost end gives you leftmost-longest. two linear DFA scans, no backtracking, no ambiguity.<p>I&#x27;m pretty sure that should say &quot;the leftmost start paired with the <i>leftmost</i> end&quot;.<p>This also implies that the algorithm has to scan the entire input to find the first match, and the article goes on to confirm this. So the algorithm is a poor choice if you just want the first match in a very long text. But if you want all matches it is very good.
    • mananaysiempre4 hours ago
      &gt; I&#x27;m pretty sure that should say &quot;the leftmost start paired with the leftmost end&quot;.<p>I’m pretty sure it shouldn’t, that would give you the leftmost shortest match instead of leftmost longest.
      • masfuerte4 hours ago
        As originally written, doesn&#x27;t it go from the start of the first match to the end of the last match? I feel like I&#x27;m missing something.
        • ieviev3 hours ago
          It goes from start of the first match to the longest &quot;alive&quot; end, in practice it will go to a dead state and return after finding the match end.<p>there&#x27;s an implicit `.*` in front of the first pass but i felt it would&#x27;ve been a long tangent so i didn&#x27;t want to get into it.<p>so given input &#x27;aabbcc&#x27; and pattern `b+`,<p>first reverse pass (using `.*b+`) marks &#x27;aa|b|bcc&#x27;&lt;-<p>the forward pass starts from the first match:<p>&#x27;aa-&gt;b|b|cc&#x27; marking 2 ends<p>then enters a dead state after the first &#x27;c&#x27; and returns the longest end: aa|bb|cc<p>i hope this explains it better
          • masfuerte2 hours ago
            Cheers. I was more confused by how you were doing multiple matches. So I read the paper, which describes the AllEnds algorithm. If I understand correctly, the reverse pass captures all of the match starts and these need to be remembered for the forward pass. Which is what you were showing above, but I didn&#x27;t follow it.<p>So, once it gets going, a traditional engine can produce matches iteratively with no further allocation, but RE# requires allocation proportional to the total number of matches. And in return, it&#x27;s very much faster and much easier to use (with intersection and complement).
            • ieviev2 hours ago
              Yes, exactly correct<p>It&#x27;s also beneficial to merge some of the matching locations into ranges where possible, so when `a*` matches a long sequence of &#x27;|a|a|a|a|a|&#x27;, it can be represented as a range of (0,5), we do this to keep the lookaround internal states smaller in the engine.
        • mananaysiempre2 hours ago
          Right, the explanation seems to be a bit oversimplified, but I don’t think it’s difficult to fix it up: you need to collect non-overlapping starts (with an RTL scan) and ends (with an LTR scan) and zip them together. The non-overlapping matches are the last ones you see before you need to reset the matcher (traverse a failing edge). This feels like it should work.<p>(I tried to write some pseudocode here but got annoyed dealing with edge cases like zero-length matches at EOF, sorry.)
  • andriamanitra4 hours ago
    This is very interesting. I&#x27;m a bit skeptical about the benchmarks &#x2F; performance claims because they seem almost too good to be true but even just the extended operators alone are a nice improvement over existing regex engines.<p>The post mentions they also have a native library implemented in Rust without dependencies but I couldn&#x27;t find a link to it. Is that available somewhere? I would love to try it out in some of my projects but I don&#x27;t use .NET so the NuGET package is of no use to me.
    • ieviev4 hours ago
      There&#x27;s currently only a string solver with the same core library, but not a full regex engine <a href="https:&#x2F;&#x2F;github.com&#x2F;ieviev&#x2F;cav25-resharp-smt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ieviev&#x2F;cav25-resharp-smt</a><p>I will open source the rust engine soon as well, some time this month.<p>As for the benchmarks, it&#x27;s the fastest for large patterns and lookarounds, where leftmost-longest lets you get away with less memory usage so we don&#x27;t need to transition from DFA to NFA.<p>In the github readme benchmarks it&#x27;s faster than the exponential implementations of .NET Compiled so the 35 000x can be an arbitrary multiplier, you can keep adding alternatives and make it 1000000x.<p>for a small set of string literals it will definitely lose to Hyperscan and Rust regex since they have a high effort left-to-right SIMD algorithm that we cannot easily use.
      • burntsushi3 hours ago
        &gt; for simple string literals it will definitely lose to Hyperscan and Rust regex since they have a high effort left-to-right SIMD algorithm that we cannot easily use<p>I think &quot;simple string literals&quot; undersells it. I think that description works for engines like RE2 or Go&#x27;s regex engine, but not Hyperscan or Rust regex. (And I would put Hyperscan in another category than even Rust regex.) Granted, it is arguably difficult to be succinct here since it&#x27;s a heuristic with difficult-to-predict failure points. But something like: &quot;patterns from which a small number of string literals can be extracted.&quot;
        • ieviev3 hours ago
          yes, that is correct. also Rust&#x27;s engine matches the full unicode spec as individual characters, whereas .NET&#x27;s will chop emojis into two sometimes, so Rust at a disadvantage here.<p>something i&#x27;ve been also wondering is how does Harry (<a href="https:&#x2F;&#x2F;ieeexplore.ieee.org&#x2F;document&#x2F;10229022" rel="nofollow">https:&#x2F;&#x2F;ieeexplore.ieee.org&#x2F;document&#x2F;10229022</a>) compare to the Teddy algorithm, it&#x27;s written by some of the same authors - i wonder if it&#x27;s used in any engines outside of Hyperscan today.
      • feldrim3 hours ago
        Would SearchValues&lt;char&gt; help there for a fallback to a SIMD optimized simple string literal search rather than the happy path?
        • ieviev3 hours ago
          Yes, that&#x27;s exactly what we did to be competitive in the benchmarks.<p>There&#x27;s a lot of simple cases where you don&#x27;t really need a regex engine at all.<p>integrating SearchValues as a multi-string prefix search is a bit harder since it doesn&#x27;t expose which branch matched so we would be taking unnecessary steps.<p>Also .NET implementation of Hyperscan&#x27;s Teddy algorithm only goes left to right.. if it went right to left it would make RE# much faster for these cases.
    • sgc3 hours ago
      I second this request. It would be wonderful to be able to test the rust implementation since it easier to call from other languages in my typical setup. I have a couple uses cases I have never fully resolved, just implemented partial work arounds and accepted a restricted feature set. This would probably allow me to deal with them correctly.
  • anentropic5 hours ago
    Fantastic stuff!<p>FYI some code snippets are unreadable in &#x27;light mode&#x27; (&quot;what substrings does the regex (a|ab)+ match in the following input?&quot;)
    • ieviev5 hours ago
      ah thank you for letting me know, fixed it now!
  • feldrim4 hours ago
    You got me at TalTech. Great job and the paper is high quality. I&#x27;ll have to learn F# but I believe it is worth it.
  • andix3 hours ago
    It should be possible to directly compile this library to native code and use it in any other language. Maybe adding some C-style wrappers will be needed.
  • nanoxide2 hours ago
    Calling this RE# (resharp), when there is a much more popular and established product already named R# (ReSharper, by JetBrains) in the. NET world will probably hurting your SEO and&#x2F;or could potentially cause some legal grief.
  • FrustratedMonky4 hours ago
    F# is one of the biggest &#x27;What could have beens&#x27;. Great language, that just didn&#x27;t hit the right time, or reach critical mass of the gestalt of the community.
    • nbevans3 hours ago
      It uses far less tokens than C#, so watch this space...
      • delta_p_delta_x58 minutes ago
        Ml-family languages (and frankly, all natively functional languages) are just incredibly terse and information-dense second only to stuff like APL. And yet when written idiomatically and with good object and type naming they are surprisingly readable and writeable.<p>&#x27;Twas a bad idea to train LLMs on the corpus of leaky, verbose C and C++ first instead of on these strict, strongly-typed, highly structural languages.
      • nodra3 hours ago
        Care to explain? Pattern matching, type inference, etc.?
        • Nelkins2 hours ago
          Various investigations have found it to be one of the most token efficient statically typed programming languages<p><a href="https:&#x2F;&#x2F;martinalderson.com&#x2F;posts&#x2F;which-programming-languages-are-most-token-efficient&#x2F;" rel="nofollow">https:&#x2F;&#x2F;martinalderson.com&#x2F;posts&#x2F;which-programming-languages...</a>
        • balakk2 hours ago
          It&#x27;s all about the goddamned machines.. since F# is terse, they figure agent-generated F# code is cheaper.
          • KurtMueller2 hours ago
            I like to think of F# as concise.
  • meindnoch4 hours ago
    @burnsushi is that true?
    • keybored4 hours ago
      Tentative doubt until&#x2F;if he confirms. (but specifically in F# though. Edit: No, comparisons to Rust etc. are made in TFA)
  • sourcegrift5 hours ago
    [flagged]