Skip to main content

PgRegex

Struct PgRegex 

Source
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

Source

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.

Source

pub fn is_match(&self, text: &str) -> bool

Whether text ~ pattern holds, as textregexeq without flags answers, a match anywhere in text, ^ and $ anchoring at its ends alone, and . and a negated bracket expression matching a newline.

Trait Implementations§

Source§

impl Clone for PgRegex

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PgRegex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for PgRegex

§

impl Freeze for PgRegex
where *const u8 is !null: Freeze, PhantomData<Step>: Freeze, PhantomData<Set>: Freeze,

§

impl Move for PgRegex

§

impl RefUnwindSafe for PgRegex

§

impl Send for PgRegex
where RawVecInner: Send,

§

impl Sync for PgRegex
where RawVecInner: Sync,

§

impl Unpin for PgRegex
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for PgRegex

§

impl UnwindSafe for PgRegex
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.