pub struct PyRegex { /* private fields */ }Expand description
A compiled Python-dialect pattern.
Owns the underlying fancy-regex engine and does not expose it. That is
deliberate: a third-party type in a public signature is only the same type
as the consumer’s when cargo hands both sides one copy, which makes the
version requirement on that dependency a source-compatibility surface
rather than a private choice. Wrapping it moves the requirement back where
it belongs — see “Dependencies in the public API” in the crate docs (#177).
The surface here is what this crate needs plus what reading a match
requires; it is not a re-implementation of the engine. A caller that wants
the engine itself should depend on fancy-regex directly and pick its own
version — this crate deliberately does not re-export ours, because nothing
here hands out one of its types for the two to have to agree about.
let re = termproof::pyregex::compile(r"(?<n>\d+)").unwrap();
let caps = re.captures("abc 42").unwrap().unwrap();
assert_eq!(caps.get(0).unwrap().as_str(), "42");
assert_eq!(caps.name("n").unwrap().as_str(), "42");Implementations§
Source§impl PyRegex
impl PyRegex
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
The translated pattern the engine was built from.
This is the fancy-regex form, not the Python source: translate
rewrites \Z and rejects what Python rejects, so the two differ
wherever a rewrite applied.
Kept as our own copy rather than read back from the engine. The engine’s
as_str is not stable across its own patch releases — x\z reads back
as x$ on fancy-regex 0.16.0 and as x\z on 0.16.2 — so forwarding
to it would put an engine-version-dependent answer in this crate’s
public API, which is the same class of leak as putting its types there.
Found by the test at the declared dependency floors CI step (#177).
Sourcepub fn is_match(&self, haystack: &str) -> Result<bool, String>
pub fn is_match(&self, haystack: &str) -> Result<bool, String>
Whether the pattern matches anywhere in haystack.
Err when the backtracking engine gives up on a pathological pattern.
That is not a match and not a reason to end a run; callers here treat it
as “no match” and carry on.
Sourcepub fn captures<'h>(
&self,
haystack: &'h str,
) -> Result<Option<PyCaptures<'h>>, String>
pub fn captures<'h>( &self, haystack: &'h str, ) -> Result<Option<PyCaptures<'h>>, String>
The capture groups of the leftmost match, or None when there is none.
Err carries the same meaning as on is_match.
Sourcepub fn capture_names(&self) -> impl Iterator<Item = Option<&str>> + '_
pub fn capture_names(&self) -> impl Iterator<Item = Option<&str>> + '_
Every capture group’s name in group order, None for the unnamed ones.
Group 0 is the whole match and is never named, so the first item is
always None.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PyRegex
impl RefUnwindSafe for PyRegex
impl Send for PyRegex
impl Sync for PyRegex
impl Unpin for PyRegex
impl UnsafeUnpin for PyRegex
impl UnwindSafe for PyRegex
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.