pub struct PgRegex { /* private fields */ }Expand description
A pattern of a subset of PostgreSQL 18’s advanced regular expressions, compiled once to an
automaton that answers text ~ pattern without flags for any text.
The subset holds literal characters, \ before an ASCII punctuation character, ., bracket
expressions of characters, ranges and character classes, ^, $, capturing and (?:
groups, alternation, and *, +, ?, {m}, {m,} and {m,n}, greedy or not. PostgreSQL
compiles every pattern of the subset, and a pattern holding anything else is refused with the
RegexError naming its first construct the subset does not take.
The classes [:alnum:], [:alpha:], [:ascii:], [:blank:], [:cntrl:], [:digit:],
[:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], [:word:] and
[:xdigit:] match as in a UTF-8 database whose LC_CTYPE is glibc 2.41’s en_US.utf8, the
locale of the postgres:18.6 Docker image, so [:space:] holds U+3000 and not U+00A0.
use sql_values::postgres::{PgRegex, RegexError};
let list = PgRegex::new(
r"^[a-z_][a-z0-9_]*:(read|insert|update|delete)(,[a-z_][a-z0-9_]*:(read|insert|update|delete))*$",
)
.unwrap();
assert!(list.is_match("resources:read,notes:insert"));
assert!(!list.is_match("resources:read,"));
let url = PgRegex::new("^https://[^[:space:]/?#]+").unwrap();
assert!(url.is_match("https://example.com/notes"));
assert!(!url.is_match("https://\u{3000}example.com"));
assert!(url.is_match("https://\u{a0}example.com"));
assert_eq!(PgRegex::new(r"\d+").unwrap_err(), RegexError::ClassEscape);
assert_eq!(PgRegex::new("(?=a)").unwrap_err(), RegexError::Lookaround);
assert_eq!(PgRegex::new("***=a").unwrap_err(), RegexError::Director);Implementations§
Source§impl PgRegex
impl PgRegex
Sourcepub fn new(pattern: &str) -> Result<Self, RegexError>
pub fn new(pattern: &str) -> Result<Self, RegexError>
Compile pattern.
§Errors
The RegexError naming the first construct of pattern outside the subset, or
RegexError::TooLarge for a pattern past the subset’s size limit.