<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio" -->

---
title: Labeled matches: why is this not in every regex engine?
description: A Rust regex engine called resharp implements 'labeled matches' (also known as regex sets with tagged categorization), letting multiple patterns be matched...
canonical: https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Labeled matches: why is this not in every regex engine? | daily.dev
og:description: A Rust regex engine called resharp implements 'labeled matches' (also known as regex sets with tagged categorization), letting multiple patterns be matched...
og:url: https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio
og:image: https://api.daily.dev/og/posts/LpFQh8kio.png
og:image:alt: Labeled matches: why is this not in every regex engine?
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Labeled matches: why is this not in every regex engine?

**[Lobsters](https://daily.dev/sources/lobsters)** · 9 min read · 0 upvotes · 0 comments

## Summary

A Rust regex engine called resharp implements 'labeled matches' (also known as regex sets with tagged categorization), letting multiple patterns be matched simultaneously with each match tagged by which pattern it satisfies, at essentially no extra runtime cost over a normal regex search. The author benchmarks it against spaCy's NER model, claiming 1.92 GB/s throughput versus spaCy's 0.43 MB/s (a roughly 4500x difference) on a labeling task, though acknowledging the comparison is not apples-to-apples. The piece also demonstrates using regex boolean algebra (intersection, complement, if-then-else composition) to build a fully correct Gregorian calendar date validator (including leap years) as a single regex that compiles to only 32 DFA states, and simplifies down to 599 characters. It closes with a discussion of overlap/subsumption checking between regex patterns, referencing the author's CAV 2025 paper on regex decision procedures in Extended RE#, and shows how to use the resharp RegexSet API to enforce disjoint label sets.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://iev.ee/blog/categorize-everything-all-at-once>

## Questions this post answers

### How can I match multiple regex patterns at once and know which pattern each match came from without extra runtime cost?

Labeled matches attach a label (tag) to the final states of a combined regex state machine, so identifying which pattern matched becomes a free array lookup from state to label after the state machine executes. This is implemented in the resharp Rust crate's RegexSet API under the regex_set feature, using categorize_all to return matches paired with their originating pattern index, with performance reported at roughly 1.92 GB/s on 8 threads across 10 patterns.

_daily.dev surfaces deep dives like this for developers optimizing multi-pattern text extraction pipelines._

### Can a single regex validate a full Gregorian calendar date including leap years without external code?

Yes, a regex built using intersection, complement, and if-then-else composition can validate yyyy-MM-dd dates including leap years as a pure regular language. A 24,504-character generated pattern encoding every month length and leap year rule compiles to only 32 DFA states, and after internal simplification the equivalent pattern shrinks to 599 characters while automatically rediscovering the leap year rule.

_daily.dev helps developers tracking regex and formal-language techniques stay ahead of edge cases like calendar validation._

### How do I check whether two regex patterns overlap or if one regex subsumes another?

Overlap and subsumption between regular languages can be checked using boolean set operations: intersecting two patterns and testing if the result is an empty language reveals overlap, while a subsumption check (A ⊆ B) tells whether every match of one pattern is also a match of another. The resharp crate exposes mk_inter, is_empty_lang, and subsumes functions for this, based on research published as 'Regex Decision Procedures in Extended RE#' at CAV 2025.

_developers verifying disjoint regex label sets can find implementation-level detail like this through daily.dev._

## Community take

How the wider developer community reacted, aggregated from 3 discussions and 12 comments across lobsters, hackernews (as of 2026-09-18).

**TL;DR:** Discussion centers on the date-validation regex example, with deep debate over what counts as a truly correct date matcher (leap years, locale/historical calendar quirks, lookarounds) alongside genuine interest in benchmarking the engine against other fast regex implementations like JavelinPattern.

**Sentiment:** 20% positive · 50% mixed · 30% skeptical

**The case for**

- Some found the topic of encoding date validity as a regular grammar genuinely interesting to explore.
- People noted the regex avoids common pitfalls like `\d` matching non-ASCII Unicode digits.
- There's interest in the engine's raw performance, especially on complex patterns like Unicode/dictionary matching where it shows large speedups.
- Commenters suggested and appreciated cross-benchmarking against other fast engines like JavelinPattern via rebar.

**The pushback**

- Several questioned whether validating dates like leap years even matters in practice since malformed dates rarely appear in real documents.
- Extended discussion argued that true date validity is locale- and history-dependent (proleptic vs. historical Gregorian calendar, adoption dates varying by region), making a single 'correct' regex ill-defined.
- One commenter argued that fully correct historical/locale-aware date matching is probably impossible in practice.

**By community**

- lobsters (mixed): A technical tangent debates the philosophical and practical correctness of regex-based date validation, alongside genuine interest in performance benchmarking against other engines.

**Hottest debate:** Whether a regex can ever be considered a fully 'correct' date validator once locale-specific and historical calendar quirks (like the Julian-to-Gregorian transition) are taken into account.

**Open questions**

- Is it actually possible to build a regex-based date validator that correctly accounts for both the proleptic Gregorian calendar and historical, locale-specific calendar transitions?
- How would the engine compare in a direct rebar benchmark against other fast engines like JavelinPattern?

**Highlights**

> > if it’s sarcasm I don't know, but I found the question moderately interesting to ponder. I, too, am guilty of `\d{4}-\d{2}-\d{2}`-style parsing of dates, despite this obviously being incorrect in general and correct only in the narrow confines of the particular datasets I am operating on (e.g., contemporary financial data with known-date-columns.) I took a moment to explore this topic. The LLM is able to produce a seemingly correct expression to match only valid dates (excluding invalid dates like year zero or February twenty-ninth in non-Leap Years.) Unfortunately, validating this result is significantly more expensive than producing it, and the whole purpose of this comment is to challenge the “looks good to me”-style of writing regular expressions. Therefore, I will not reproduce its output here. ([“Code golf”](https://codegolf.stackexchange.com/questions/54555/validate-iso-8601-by-rx), [Stack Overflow](https://stackoverflow.com/questions/24319295/date-leap-year-validation-regex), and [RegEx Lib](https://regexlib.com/REDetails.aspx?regexp_id=3344) answers to this question are readily findable. All linked answers predate LLMs.) Instead, I will share some resources so that others can independently reproduce and validate any work requiring such a regular expression. Specifically: - such an expression must match a date on the [“proleptic” Gregorian calendar](https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar) if it is to be locale-free; since the Gregorian calendar was adopted (usu. replacing the Julian calendar) at various points in history, certain dates may or may not have existed historically (i.e., prior to the calendar's introduction.) - the expression can either exclude these dates, or it must be parameterised on a locale so that it can exclude [“dates that don't exist”](https://blog.yossarian.net/2015/06/09/Dates-That-Dont-Exist); however, I am skeptical that a locale-based approach is feasible, since we have to also use contemporaneous locales (e.g., what dates existed in 1752 requires knowing if our locale is Great Britain, but the result is presumably applicable only to what constituted Great Britain in 1752.) - such an expression needs to ensure: month must be 1–12; day must be 1–31 for Jan, Mar, May, Jul, Aug, Oct, Dec and 1–30 for Apr, Jun, Sep, Nov and 1–28 for Feb except on [leap years](https://www.rfc-editor.org/info/rfc3339/#appendix-C) where should be 1–29 - whether or not year 0000 is valid depends on the calendar convention chosen—[RFC-3339](https://www.rfc-editor.org/info/rfc3339/#section-1) and [ISO-8601:2004](https://projects.csail.mit.edu/workbench/update/guides/03%20-%20Timex%20ISO%208601%20Standard.pdf) indicate that this is a valid date, as is the convention in [“Astronomical calendars”](https://aa.usno.navy.mil/faq/millennium); however, given the relatively late introduction of zero and negative numbers into our mathematics, “[In t]he civil calendar […] [t]he initial epoch (first year) of the Common Era (CE) is 1 CE also known as AD 1.” - for most actual applications, it is assumed that the date range will not only be finite but will be relatively low cardinality (e.g., for nanosecond-fidelity timestamps with signed 64-bit storage, we can store a maximum of ±106,751 days; therefore, tools like `pandas.Timestamp` allow representing dates from 1677–2262); as a result, a matching regular expression can, indeed, be trivially encoded within a regular grammar (i.e., we encode using alternation with no features like negative- and positive-lookahead assertions, thus any regular expression tool can encode this,) including those that model a locale-specific, non-proleptic Gregorian calendar. However, this representation will *not* be compact. Fundamentally, because leap-year matching, the month restrictions, and corresponding day restrictions are themselves finite (e.g., `([0-9][0-9](0[48]|[2468][048]|[13579][26])|([02468][048]|[13579][26])00)` for leap years,) the desired expression should be 
> — [dutc on lobsters · 1 points, 1 comments](https://lobste.rs/s/7d6sh9/labeled_matches_why_is_this_not_every#c_cuwqot)

> > lookarounds are entirely regular as well You're right—the reason these are not available in tools like [re2](https://github.com/google/re2/blob/main/README.md) do not support them is avoiding backtracking to meet performance guarantees rather than avoiding irregularity: “as a matter of principle, RE2 does not support constructs for which only backtracking solutions are known to exist. Thus, backreferences and look-around assertions are not supported.” (It is only the presence of back-references that cannot be modeled by finite automata and allow non-regular grammars. Relevant: [“Real world regular expressions”](https://swtch.com/~rsc/regexp/regexp2.html#real)) > If you prefix the date by a certain locale, and the date regex for each locale on its own is regular, the whole expression should be regular. I suspect that this problem is not actually solvable in any real way, even if the mathematics can be made to work as you describe.  Generally, we relegate this work to some library that will not be provided with sufficient information to make a correct determination, especially considering that said information is probably not available at any upper layer except the top-most. In other words, even for every possible match we return, we qualify the match on locale-specific information for which the match is valid, we would need complete and authoritative locale information, which I don't think exists in any readily available form. (Such a list may not even exist at all, as it relies on historical information that may be lost.) To make a determination on whether said matches are valid for a given locale, we would then have to correlate these to the historical jurisdiction, which may not be readily available, may not be available at all, or may have some additional considerations (like historical jurisdictional conflict.) This means that, as a practical matter, it is *probably impossible* to create the *one, true date matching library* that works in all circumstances for both proleptic and historical Gregorian calendars. Of course, I doubt anyone cares, because people who will care about correctness in the face of, say, historical jurisdictional conflicts will likely carry that care over into using custom tools that encode (likely just) the constraints of the jurisdiction they are interested in, and they are likely to have sufficient resources to solve this problem adequately for their purposes. (As ~~a computer programmer~~ someone who does computer programming, who also happens to be a human being, I am actually very enthused to know that these unsolvable problems exist. It is an affirmation that the technology must necessarily be subordinate to our humanity.)
> — [dutc on lobsters · 1 points, 1 comments](https://lobste.rs/s/7d6sh9/labeled_matches_why_is_this_not_every#c_jsxwas)

> It's quite vague what a dates are valid if you look at it this way. Now we need to form a regular expression for a date within a 64 bit unix timestamp haha. I wanted to add that for purposes of a boolean yes/no, lookarounds are entirely regular as well. You can eliminate them as either concatenation or intersection. eg: `.*a(?=b).*`  === `.*ab.*` `.*a(?=b)(?=c).*`  === `.*a(b.*&c.*)` `.*a(?!b).*`  === `.*a~(b.*\z).*` A leap year rule above year 10000+ should still be regular too, since it tracks divisibility by 400.. If you prefix the date by a certain locale, and the date regex for each locale on its own is regular, the whole expression should be regular. I'm thinking it should be possible.. will it blow up in size? will the state machine blow up in size? Not sure.
> — [iev on lobsters · 1 points, 1 comments](https://lobste.rs/s/7d6sh9/labeled_matches_why_is_this_not_every#c_r8tunn)

> Have you thought about trying to add RE# to [rebar](https://github.com/BurntSushi/rebar)? I'd be curious to see a performance comparison between it and [JavelinPattern](https://github.com/BurntSushi/rebar/pull/34).
> — [sunshine on lobsters · 3 points, 3 comments](https://lobste.rs/s/7d6sh9/labeled_matches_why_is_this_not_every#c_rtmukf)

> [Benchmarked again now](https://github.com/ieviev/rebar), looks like it got slightly slower on chinese and russian prose text when we changed byte frequency tables to optimize for code search instead. Not sure if it matters much. The main place you really see a big difference is the nasty large patterns eg. context, complex unicode, dictionary etc. There are few such patterns in the benchmark (see context 605.0 MB/s vs 83.8 MB/s). [The paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2025/01/popl25-p2-final.pdf) shows rebar evaluation of the earlier .NET version, which should give a good overview. Recently we've been optimizing more for popular patterns across crates.io / npm which you can see in the end of the repo readme. A bit of research told me that almost half of regex usage is just `\s+` and `\d+` and similar, so today RE# is 2x and 4x faster on these patterns.
> — [iev on lobsters · 1 points](https://lobste.rs/s/7d6sh9/labeled_matches_why_is_this_not_every#c_w1ookr)

**Source threads**

- [lobsters](https://lobste.rs/s/7d6sh9/labeled_matches_why_is_this_not_every) · 27 points · 12 comments
- [hackernews](https://news.ycombinator.com/item?id=49723587) · 4 points · 0 comments
- [hackernews](https://news.ycombinator.com/item?id=49743888) · 3 points · 0 comments

## Similar posts on daily.dev

- [yes, all longest regex matches in linear time is possible](https://daily.dev/posts/yes-all-longest-regex-matches-in-linear-time-is-possible-3i3fe1s8z) · Lobsters · 1 upvotes · 0 comments
- [RE\#: how we built the world's fastest regex engine in F\#](https://daily.dev/posts/re-how-we-built-the-world-s-fastest-regex-engine-in-f--s1rdfweqf) · Hacker News · 4 upvotes · 0 comments
- [what 262,715 regex questions on stack overflow haven't answered](https://daily.dev/posts/what-262-715-regex-questions-on-stack-overflow-haven-t-answered-p5elhkiyv) · Lobsters · 4 upvotes · 0 comments
- [finding all regex matches has always been O\(n²\). even in the engines built to prevent it](https://daily.dev/posts/finding-all-regex-matches-has-always-been-o-n-even-in-the-engines-built-to-prevent-it-6av6qyuxr) · Hacker News · 4 upvotes · 0 comments
- [Regex Are Not the Problem. Strings Are.](https://daily.dev/posts/regex-are-not-the-problem-strings-are--lvxxdmy46) · ITNEXT · 0 upvotes · 0 comments

---

Tags: [#rust](https://daily.dev/tags/rust), [#nlp](https://daily.dev/tags/nlp)

[View this post on daily.dev](https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Labeled matches: why is this not in every regex engine?","url":"https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio"},"datePublished":"2026-09-18T13:26:01.917Z","dateModified":"2026-09-18T21:28:42.412Z","description":"A Rust regex engine called resharp implements 'labeled matches' (also known as regex sets with tagged categorization), letting multiple patterns be matched...","image":"https://media.daily.dev/image/upload/s--VDukGCjf--/f_auto/v1722860399/public/Placeholder%2002","thumbnailUrl":"https://media.daily.dev/image/upload/s--VDukGCjf--/f_auto/v1722860399/public/Placeholder%2002","isAccessibleForFree":true,"articleSection":"Lobsters","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Lobsters","logo":"https://media.daily.dev/image/upload/s--tl8v_Fku--/f_auto,t_logo/v1698841318/logos/lobste.jpg","url":"https://daily.dev/sources/lobsters"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"rust,nlp","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Lobsters","item":"https://daily.dev/sources/lobsters"},{"@type":"ListItem","position":3,"name":"Labeled matches: why is this not in every regex engine?"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/labeled-matches-why-is-this-not-in-every-regex-engine--lpfqh8kio#faq","mainEntity":[{"@type":"Question","name":"How can I match multiple regex patterns at once and know which pattern each match came from without extra runtime cost?","acceptedAnswer":{"@type":"Answer","text":"Labeled matches attach a label (tag) to the final states of a combined regex state machine, so identifying which pattern matched becomes a free array lookup from state to label after the state machine executes. This is implemented in the resharp Rust crate's RegexSet API under the regex_set feature, using categorize_all to return matches paired with their originating pattern index, with performance reported at roughly 1.92 GB/s on 8 threads across 10 patterns. daily.dev surfaces deep dives like this for developers optimizing multi-pattern text extraction pipelines."}},{"@type":"Question","name":"Can a single regex validate a full Gregorian calendar date including leap years without external code?","acceptedAnswer":{"@type":"Answer","text":"Yes, a regex built using intersection, complement, and if-then-else composition can validate yyyy-MM-dd dates including leap years as a pure regular language. A 24,504-character generated pattern encoding every month length and leap year rule compiles to only 32 DFA states, and after internal simplification the equivalent pattern shrinks to 599 characters while automatically rediscovering the leap year rule. daily.dev helps developers tracking regex and formal-language techniques stay ahead of edge cases like calendar validation."}},{"@type":"Question","name":"How do I check whether two regex patterns overlap or if one regex subsumes another?","acceptedAnswer":{"@type":"Answer","text":"Overlap and subsumption between regular languages can be checked using boolean set operations: intersecting two patterns and testing if the result is an empty language reveals overlap, while a subsumption check (A ⊆ B) tells whether every match of one pattern is also a match of another. The resharp crate exposes mk_inter, is_empty_lang, and subsumes functions for this, based on research published as 'Regex Decision Procedures in Extended RE#' at CAV 2025. developers verifying disjoint regex label sets can find implementation-level detail like this through daily.dev."}}]}
```

