Crate regex_specificity

Crate regex_specificity 

Source
Expand description

§regex-specificity

This crate provides a heuristic-based approach to calculate the specificity of a regular expression pattern against a specific string.

§Concept

Specificity measures how “precise” a match is. For example, the pattern abc is more specific to the string “abc” than the pattern a.c or .*.

The calculation follows these principles:

  1. Positional Weighting: Earlier matches contribute more to the total specificity than later ones.
  2. Certainty: Literals (exact characters) specificity higher than character classes or wildcards.
  3. Information Density: Narrower character classes (e.g., [a-z]) specificity higher than broader ones (e.g., .).
  4. Branching Penalty: Patterns with many alternatives (alternations) are penalized as they are less specific.

Functions§

get
Calculates the specificity of a pattern against a given string.