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
impl Expander
sourcepub fn python() -> Expander
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 \\
.
sourcepub fn check(&self, template: &str, regex: &Regex) -> Result<()>
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
sourcepub fn escape<'a>(&self, text: &'a str) -> Cow<'a, str>
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.",
);
sourcepub fn expansion(&self, template: &str, captures: &Captures<'_>) -> String
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
.
sourcepub fn append_expansion(
&self,
dst: &mut String,
template: &str,
captures: &Captures<'_>,
)
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.