Struct Builder
struct Builder { ... }
A builder for configuring and constructing a Regex.
The builder permits configuring two different aspects of a Regex:
Builder::configurewill set high-level configuration options as described by aConfig.Builder::syntaxwill set the syntax level configuration options as described by autil::syntax::Config. This only applies when building aRegexfrom pattern strings.
Once configured, the builder can then be used to construct a Regex from
one of 4 different inputs:
Builder::buildcreates a regex from a single pattern string.Builder::build_manycreates a regex from many pattern strings.Builder::build_from_hircreates a regex from aregex-syntax::Hirexpression.Builder::build_many_from_hircreates a regex from manyregex-syntax::Hirexpressions.
The latter two methods in particular provide a way to construct a fully
feature regular expression matcher directly from an Hir expression
without having to first convert it to a string. (This is in contrast to the
top-level regex crate which intentionally provides no such API in order
to avoid making regex-syntax a public dependency.)
As a convenience, this builder may be created via Regex::builder, which
may help avoid an extra import.
Example: change the line terminator
This example shows how to enable multi-line mode by default and change the line terminator to the NUL byte:
use ;
let re = builder
.syntax
.configure
.build?;
let hay = "\x00foo\x00";
assert_eq!;
# Ok::
Example: disable UTF-8 requirement
By default, regex patterns are required to match UTF-8. This includes regex patterns that can produce matches of length zero. In the case of an empty match, by default, matches will not appear between the code units of a UTF-8 encoded codepoint.
However, it can be useful to disable this requirement, particularly if
you're searching things like &[u8] that are not known to be valid UTF-8.
use ;
let mut builder = builder;
// Disables the requirement that non-empty matches match UTF-8.
builder.syntax;
// Disables the requirement that empty matches match UTF-8 boundaries.
builder.configure;
// We can match raw bytes via \xZZ syntax, but we need to disable
// Unicode mode to do that. We could disable it everywhere, or just
// selectively, as shown here.
let re = builder.build?;
let hay = b"\xFFfoo\xFF";
assert_eq!;
// We can also match between code units.
let re = builder.build?;
let hay = "☃";
assert_eq!;
# Ok::
Implementations
impl Builder
fn new() -> BuilderCreates a new builder for configuring and constructing a
Regex.fn build(self: &Self, pattern: &str) -> Result<Regex, BuildError>Builds a
Regexfrom a single pattern string.If there was a problem parsing the pattern or a problem turning it into a regex matcher, then an error is returned.
Example
This example shows how to configure syntax options.
use ; let re = builder .syntax .build?; let hay = "\r\nfoo\r\n"; assert_eq!; # Ok::fn build_many<P: AsRef<str>>(self: &Self, patterns: &[P]) -> Result<Regex, BuildError>Builds a
Regexfrom many pattern strings.If there was a problem parsing any of the patterns or a problem turning them into a regex matcher, then an error is returned.
Example: finding the pattern that caused an error
When a syntax error occurs, it is possible to ask which pattern caused the syntax error.
use ; let err = builder .build_many .unwrap_err; assert_eq!;Example: zero patterns is valid
Building a regex with zero patterns results in a regex that never matches anything. Because this routine is generic, passing an empty slice usually requires a turbo-fish (or something else to help type inference).
use ; let re = builder .?; assert_eq!; # Ok::fn build_from_hir(self: &Self, hir: &Hir) -> Result<Regex, BuildError>Builds a
Regexdirectly from anHirexpression.This is useful if you needed to parse a pattern string into an
Hirfor other reasons (such as analysis or transformations). This routine permits building aRegexdirectly from theHirexpression instead of first converting theHirback to a pattern string.When using this method, any options set via
Builder::syntaxare ignored. Namely, the syntax options only apply when parsing a pattern string, which isn't relevant here.If there was a problem building the underlying regex matcher for the given
Hir, then an error is returned.Example
This example shows how one can hand-construct an
Hirexpression and build a regex from it without doing any parsing at all.use ; // (?Rm)^foo$ let hir = concat; let re = builder .build_from_hir?; let hay = "\r\nfoo\r\n"; assert_eq!; Ok::fn build_many_from_hir<H: Borrow<Hir>>(self: &Self, hirs: &[H]) -> Result<Regex, BuildError>Builds a
Regexdirectly from manyHirexpressions.This is useful if you needed to parse pattern strings into
Hirexpressions for other reasons (such as analysis or transformations). This routine permits building aRegexdirectly from theHirexpressions instead of first converting theHirexpressions back to pattern strings.When using this method, any options set via
Builder::syntaxare ignored. Namely, the syntax options only apply when parsing a pattern string, which isn't relevant here.If there was a problem building the underlying regex matcher for the given
Hirexpressions, then an error is returned.Note that unlike
Builder::build_many, this can only fail as a result of building the underlying matcher. In that case, there is no singleHirexpression that can be isolated as a reason for the failure. So if this routine fails, it's not possible to determine whichHirexpression caused the failure.Example
This example shows how one can hand-construct multiple
Hirexpressions and build a single regex from them without doing any parsing at all.use ; // (?Rm)^foo$ let hir1 = concat; // (?Rm)^bar$ let hir2 = concat; let re = builder .build_many_from_hir?; let hay = "\r\nfoo\r\nbar"; let got: = re.find_iter.collect; let expected = vec!; assert_eq!; Ok::fn configure(self: &mut Self, config: Config) -> &mut BuilderConfigure the behavior of a
Regex.This configuration controls non-syntax options related to the behavior of a
Regex. This includes things like whether empty matches can split a codepoint, prefilters, line terminators and a long list of options for configuring which regex engines the meta regex engine will be able to use internally.Example
This example shows how to disable UTF-8 empty mode. This will permit empty matches to occur between the UTF-8 encoding of a codepoint.
use ; let re = new?; let got: = re.find_iter.collect; // Matches only occur at the beginning and end of the snowman. assert_eq!; let re = builder .configure .build?; let got: = re.find_iter.collect; // Matches now occur at every position! assert_eq!; Ok::fn syntax(self: &mut Self, config: Config) -> &mut BuilderConfigure the syntax options when parsing a pattern string while building a
Regex.These options only apply when
Builder::buildorBuilder::build_manyare used. The other build methods acceptHirvalues, which have already been parsed.Example
This example shows how to enable case insensitive mode.
use ; let re = builder .syntax .build?; assert_eq!; Ok::
impl Clone for Builder
fn clone(self: &Self) -> Builder
impl Debug for Builder
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnsafeUnpin for Builder
impl UnwindSafe for Builder
impl<T> Any for Builder
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Builder
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Builder
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Builder
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Builder
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Builder
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Builder
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Builder
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Builder
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>