Skip to main content

reverse_pattern

Function reverse_pattern 

Source
pub fn reverse_pattern(
    pattern: &str,
    captures: &HashMap<String, String>,
) -> Option<String>
Expand description

Best-effort reverse of an anchored regex pattern: substitute captures into each (?P<name>…) group and emit the literal segments between them. Returns None when the pattern can’t be reversed unambiguously — a non-named group, a quantified group, a character class / metacharacter in the literal text (\d, ., +, [, |, …), or a capture with no supplied value.

This covers the routable shape people actually write — ^archive/(?P<year>\d{4})/(?P<month>\d{2})$ — and bows out of the genuinely-ambiguous ones rather than guessing.