Skip to main content

anchor_pattern

Function anchor_pattern 

Source
pub fn anchor_pattern(pattern: &str) -> String
Expand description

Validate that the value matches the given regex pattern.

The regex parameter is a pre-compiled Regex object (managed by an OnceLock static in the generated code). The pattern_str parameter is the raw regex string, included in error messages for debugging.

The pattern is matched against the entire string — it is automatically anchored. If the original regex does not anchor, this function still checks that the full string matches (via is_match). If partial matching is desired, the regex should use .* appropriately. Ensure a regex pattern matches the FULL string (not partial).

If the pattern doesn’t start with ^ and end with $, it’s wrapped in ^(?:...)$ to ensure full-string matching. This prevents patterns like [0-9]{5} from matching within “abc12345xyz”.