Struct fancy_regex::Expander

source ·
pub struct Expander { /* private fields */ }
Expand description

A set of options for expanding a template string using the contents of capture groups.

Implementations§

source§

impl Expander

source

pub fn python() -> Expander

Returns an expander that uses Python-compatible syntax.

Expands all instances of \num or \g<name> in replacement to the corresponding capture group num or name, and writes them to the dst buffer given.

name may be an integer corresponding to the index of the capture group (counted by order of opening parenthesis where \0 is the entire match) or it can be a name (consisting of letters, digits or underscores) corresponding to a named capture group.

num must be an integer corresponding to the index of the capture group.

If num or name isn’t a valid capture group (whether the name doesn’t exist or isn’t a valid index), then it is replaced with the empty string.

The longest possible number is used. e.g., \10 looks up capture group 10 and not capture group 1 followed by a literal 0.

To write a literal \, use \\.

source

pub fn check(&self, template: &str, regex: &Regex) -> Result<()>

Checks template for errors. The following conditions are checked for:

  • A reference to a numbered group that does not exist in regex
  • A reference to a numbered group (other than 0) when regex contains named groups
  • A reference to a named group that does not occur in regex
  • An opening group name delimiter without a closing delimiter
  • Using an empty string as a group name
source

pub fn escape<'a>(&self, text: &'a str) -> Cow<'a, str>

Escapes the substitution character in text so it appears literally in the output of expansion.

assert_eq!(
    fancy_regex::Expander::default().escape("Has a literal $ sign."),
    "Has a literal $$ sign.",
);
source

pub fn expansion(&self, template: &str, captures: &Captures<'_>) -> String

Expands the template string template using the syntax defined by this expander and the values of capture groups from captures.

source

pub fn append_expansion( &self, dst: &mut String, template: &str, captures: &Captures<'_> )

Appends the expansion produced by expansion to dst. Potentially more efficient than calling expansion directly and appending to an existing string.

source

pub fn write_expansion( &self, dst: impl Write, template: &str, captures: &Captures<'_> ) -> Result<()>

Writes the expansion produced by expansion to dst. Potentially more efficient than calling expansion directly and writing the result.

source

pub fn write_expansion_vec( &self, dst: &mut Vec<u8>, template: &str, captures: &Captures<'_> ) -> Result

Writes the expansion produced by expansion to dst. Potentially more efficient than calling expansion directly and writing the result.

Trait Implementations§

source§

impl Debug for Expander

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Expander

source§

fn default() -> Self

Returns the default expander used by Captures::expand.

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.