Struct RegexSet
pub struct RegexSet { /* private fields */ }
Expand description
Match multiple, possibly overlapping, regexes in a single search.
A regex set corresponds to the union of zero or more regular expressions.
That is, a regex set will match a haystack when at least one of its
constituent regexes matches. A regex set as its formulated here provides a
touch more power: it will also report which regular expressions in the
set match. Indeed, this is the key difference between regex sets and a
single Regex
with many alternates, since only one alternate can match at
a time.
For example, consider regular expressions to match email addresses and
domains: [a-z]+@[a-z]+\.(com|org|net)
and [a-z]+\.(com|org|net)
. If a
regex set is constructed from those regexes, then searching the haystack
foo@example.com
will report both regexes as matching. Of course, one
could accomplish this by compiling each regex on its own and doing two
searches over the haystack. The key advantage of using a regex set is
that it will report the matching regexes using a single pass through the
haystack. If one has hundreds or thousands of regexes to match repeatedly
(like a URL router for a complex web application or a user agent matcher),
then a regex set can realize huge performance gains.
§Limitations
Regex sets are limited to answering the following two questions:
- Does any regex in the set match?
- If so, which regexes in the set match?
As with the main Regex
type, it is cheaper to ask (1)
instead of (2) since the matching engines can stop after the first match
is found.
You cannot directly extract Match
or
Captures
objects from a regex set. If you need these
operations, the recommended approach is to compile each pattern in the set
independently and scan the exact same haystack a second time with those
independently compiled patterns:
use regex::{Regex, RegexSet};
let patterns = ["foo", "bar"];
// Both patterns will match different ranges of this string.
let hay = "barfoo";
// Compile a set matching any of our patterns.
let set = RegexSet::new(patterns).unwrap();
// Compile each pattern independently.
let regexes: Vec<_> = set
.patterns()
.iter()
.map(|pat| Regex::new(pat).unwrap())
.collect();
// Match against the whole set first and identify the individual
// matching patterns.
let matches: Vec<&str> = set
.matches(hay)
.into_iter()
// Dereference the match index to get the corresponding
// compiled pattern.
.map(|index| ®exes[index])
// To get match locations or any other info, we then have to search the
// exact same haystack again, using our separately-compiled pattern.
.map(|re| re.find(hay).unwrap().as_str())
.collect();
// Matches arrive in the order the constituent patterns were declared,
// not the order they appear in the haystack.
assert_eq!(vec!["foo", "bar"], matches);
§Performance
A RegexSet
has the same performance characteristics as Regex
. Namely,
search takes O(m * n)
time, where m
is proportional to the size of the
regex set and n
is proportional to the length of the haystack.
§Trait implementations
The Default
trait is implemented for RegexSet
. The default value
is an empty set. An empty set can also be explicitly constructed via
RegexSet::empty
.
§Example
This shows how the above two regexes (for matching email addresses and domains) might work:
use regex::RegexSet;
let set = RegexSet::new(&[
r"[a-z]+@[a-z]+\.(com|org|net)",
r"[a-z]+\.(com|org|net)",
]).unwrap();
// Ask whether any regexes in the set match.
assert!(set.is_match("foo@example.com"));
// Identify which regexes in the set match.
let matches: Vec<_> = set.matches("foo@example.com").into_iter().collect();
assert_eq!(vec![0, 1], matches);
// Try again, but with a haystack that only matches one of the regexes.
let matches: Vec<_> = set.matches("example.com").into_iter().collect();
assert_eq!(vec![1], matches);
// Try again, but with a haystack that doesn't match any regex in the set.
let matches: Vec<_> = set.matches("example").into_iter().collect();
assert!(matches.is_empty());
Note that it would be possible to adapt the above example to using Regex
with an expression like:
(?P<email>[a-z]+@(?P<email_domain>[a-z]+[.](com|org|net)))|(?P<domain>[a-z]+[.](com|org|net))
After a match, one could then inspect the capture groups to figure out which alternates matched. The problem is that it is hard to make this approach scale when there are many regexes since the overlap between each alternate isn’t always obvious to reason about.
Implementations§
§impl RegexSet
impl RegexSet
pub fn new<I, S>(exprs: I) -> Result<RegexSet, Error>
pub fn new<I, S>(exprs: I) -> Result<RegexSet, Error>
Create a new regex set with the given regular expressions.
This takes an iterator of S
, where S
is something that can produce
a &str
. If any of the strings in the iterator are not valid regular
expressions, then an error is returned.
§Example
Create a new regex set from an iterator of strings:
use regex::RegexSet;
let set = RegexSet::new([r"\w+", r"\d+"]).unwrap();
assert!(set.is_match("foo"));
pub fn empty() -> RegexSet
pub fn empty() -> RegexSet
Create a new empty regex set.
An empty regex never matches anything.
This is a convenience function for RegexSet::new([])
, but doesn’t
require one to specify the type of the input.
§Example
use regex::RegexSet;
let set = RegexSet::empty();
assert!(set.is_empty());
// an empty set matches nothing
assert!(!set.is_match(""));
pub fn is_match(&self, haystack: &str) -> bool
pub fn is_match(&self, haystack: &str) -> bool
Returns true if and only if one of the regexes in this set matches the haystack given.
This method should be preferred if you only need to test whether any of the regexes in the set should match, but don’t care about which regexes matched. This is because the underlying matching engine will quit immediately after seeing the first match instead of continuing to find all matches.
Note that as with searches using Regex
, the
expression is unanchored by default. That is, if the regex does not
start with ^
or \A
, or end with $
or \z
, then it is permitted
to match anywhere in the haystack.
§Example
Tests whether a set matches somewhere in a haystack:
use regex::RegexSet;
let set = RegexSet::new([r"\w+", r"\d+"]).unwrap();
assert!(set.is_match("foo"));
assert!(!set.is_match("☃"));
pub fn is_match_at(&self, haystack: &str, start: usize) -> bool
pub fn is_match_at(&self, haystack: &str, start: usize) -> bool
Returns true if and only if one of the regexes in this set matches the haystack given, with the search starting at the offset given.
The significance of the starting point is that it takes the surrounding
context into consideration. For example, the \A
anchor can only
match when start == 0
.
§Panics
This panics when start >= haystack.len() + 1
.
§Example
This example shows the significance of start
. Namely, consider a
haystack foobar
and a desire to execute a search starting at offset
3
. You could search a substring explicitly, but then the look-around
assertions won’t work correctly. Instead, you can use this method to
specify the start position of a search.
use regex::RegexSet;
let set = RegexSet::new([r"\bbar\b", r"(?m)^bar$"]).unwrap();
let hay = "foobar";
// We get a match here, but it's probably not intended.
assert!(set.is_match(&hay[3..]));
// No match because the assertions take the context into account.
assert!(!set.is_match_at(hay, 3));
pub fn matches(&self, haystack: &str) -> SetMatches
pub fn matches(&self, haystack: &str) -> SetMatches
Returns the set of regexes that match in the given haystack.
The set returned contains the index of each regex that matches in
the given haystack. The index is in correspondence with the order of
regular expressions given to RegexSet
’s constructor.
The set can also be used to iterate over the matched indices. The order of iteration is always ascending with respect to the matching indices.
Note that as with searches using Regex
, the
expression is unanchored by default. That is, if the regex does not
start with ^
or \A
, or end with $
or \z
, then it is permitted
to match anywhere in the haystack.
§Example
Tests which regular expressions match the given haystack:
use regex::RegexSet;
let set = RegexSet::new([
r"\w+",
r"\d+",
r"\pL+",
r"foo",
r"bar",
r"barfoo",
r"foobar",
]).unwrap();
let matches: Vec<_> = set.matches("foobar").into_iter().collect();
assert_eq!(matches, vec![0, 2, 3, 4, 6]);
// You can also test whether a particular regex matched:
let matches = set.matches("foobar");
assert!(!matches.matched(5));
assert!(matches.matched(6));
pub fn matches_at(&self, haystack: &str, start: usize) -> SetMatches
pub fn matches_at(&self, haystack: &str, start: usize) -> SetMatches
Returns the set of regexes that match in the given haystack.
The set returned contains the index of each regex that matches in
the given haystack. The index is in correspondence with the order of
regular expressions given to RegexSet
’s constructor.
The set can also be used to iterate over the matched indices. The order of iteration is always ascending with respect to the matching indices.
The significance of the starting point is that it takes the surrounding
context into consideration. For example, the \A
anchor can only
match when start == 0
.
§Panics
This panics when start >= haystack.len() + 1
.
§Example
Tests which regular expressions match the given haystack:
use regex::RegexSet;
let set = RegexSet::new([r"\bbar\b", r"(?m)^bar$"]).unwrap();
let hay = "foobar";
// We get matches here, but it's probably not intended.
let matches: Vec<_> = set.matches(&hay[3..]).into_iter().collect();
assert_eq!(matches, vec![0, 1]);
// No matches because the assertions take the context into account.
let matches: Vec<_> = set.matches_at(hay, 3).into_iter().collect();
assert_eq!(matches, vec![]);
pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the total number of regexes in this set.
§Example
use regex::RegexSet;
assert_eq!(0, RegexSet::empty().len());
assert_eq!(1, RegexSet::new([r"[0-9]"]).unwrap().len());
assert_eq!(2, RegexSet::new([r"[0-9]", r"[a-z]"]).unwrap().len());
pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if this set contains no regexes.
§Example
use regex::RegexSet;
assert!(RegexSet::empty().is_empty());
assert!(!RegexSet::new([r"[0-9]"]).unwrap().is_empty());
pub fn patterns(&self) -> &[String]
pub fn patterns(&self) -> &[String]
Returns the regex patterns that this regex set was constructed from.
This function can be used to determine the pattern for a match. The slice returned has exactly as many patterns givens to this regex set, and the order of the slice is the same as the order of the patterns provided to the set.
§Example
use regex::RegexSet;
let set = RegexSet::new(&[
r"\w+",
r"\d+",
r"\pL+",
r"foo",
r"bar",
r"barfoo",
r"foobar",
]).unwrap();
let matches: Vec<_> = set
.matches("foobar")
.into_iter()
.map(|index| &set.patterns()[index])
.collect();
assert_eq!(matches, vec![r"\w+", r"\pL+", r"foo", r"bar", r"foobar"]);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RegexSet
impl RefUnwindSafe for RegexSet
impl Send for RegexSet
impl Sync for RegexSet
impl Unpin for RegexSet
impl UnwindSafe for RegexSet
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<S, P, B, E>(self, other: P) -> And<T, P>
fn and<S, P, B, E>(self, other: P) -> And<T, P>
Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.