pub struct RMatch(/* private fields */);
Expand description
Implementations§
source§impl RMatch
impl RMatch
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RMatch)
if val
is a RMatch
, None
otherwise.
§Examples
use magnus::{eval, RMatch};
assert!(RMatch::from_value(eval(r#""foo".match(/o/)"#).unwrap()).is_some());
assert!(RMatch::from_value(eval(r#""o""#).unwrap()).is_none());
sourcepub fn nth_defined(self, n: isize) -> Option<bool>
pub fn nth_defined(self, n: isize) -> Option<bool>
Returns whether the n
th capture group is set.
Returns Some(true)
when there is an n
th capture and it has a value.
Returns Some(false)
when there is an n
th capture but it is empty.
Returns None
when there is no n
th capture.
This function is similar to nth_match
, but can be
used to avoid allocating a Ruby string if the value of the capture is
not needed.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new(".([a-z])([a-z]*)([0-9])?", Default::default()).unwrap();
regexp.reg_match("ex").unwrap();
let match_data = backref_get().unwrap();
// 0th group is the whole match
assert_eq!(match_data.nth_defined(0), Some(true));
// the `([a-z])` group
assert_eq!(match_data.nth_defined(1), Some(true));
// the `([a-z]*)` group
assert_eq!(match_data.nth_defined(2), Some(true));
// the `([0-9])?` group
assert_eq!(match_data.nth_defined(3), Some(false));
// no 4th group
assert_eq!(match_data.nth_defined(4), None);
sourcepub fn nth_match(self, n: isize) -> Option<RString>
pub fn nth_match(self, n: isize) -> Option<RString>
Returns the string captured by the n
th capture group.
Returns None
when there is no n
th capture.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new(".([a-z])([a-z]*)([0-9])?", Default::default()).unwrap();
regexp.reg_match("ex").unwrap();
let match_data = backref_get().unwrap();
// 0th group is the whole match
assert_eq!(
match_data.nth_match(0).map(|s| s.to_string().unwrap()),
Some(String::from("ex"))
);
// the `([a-z])` group
assert_eq!(
match_data.nth_match(1).map(|s| s.to_string().unwrap()),
Some(String::from("x"))
);
// the `([a-z]*)` group
assert_eq!(
match_data.nth_match(2).map(|s| s.to_string().unwrap()),
Some(String::from(""))
);
// the `([0-9])?` group
assert_eq!(
match_data.nth_match(3).map(|s| s.to_string().unwrap()),
None
);
// no 4th group
assert_eq!(
match_data.nth_match(4).map(|s| s.to_string().unwrap()),
None
);
sourcepub fn backref_number<T>(self, name: T) -> Result<usize, Error>where
T: IntoRString,
pub fn backref_number<T>(self, name: T) -> Result<usize, Error>where
T: IntoRString,
Returns the index for the named capture group.
Returns Err
if there’s is no named capture group with the given name.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new("Hello, (?<subject>.*)!", Default::default()).unwrap();
regexp.reg_match("Hello, World!").unwrap();
let match_data = backref_get().unwrap();
assert_eq!(match_data.backref_number("subject").unwrap(), 1);
assert!(match_data.backref_number("foo").is_err());
sourcepub fn matched(self) -> RString ⓘ
pub fn matched(self) -> RString ⓘ
Returns the string matched.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new("b(.)r", Default::default()).unwrap();
regexp.reg_match("foo bar baz").unwrap();
let match_data = backref_get().unwrap();
assert_eq!(match_data.matched().to_string().unwrap(), "bar");
sourcepub fn pre(self) -> RString ⓘ
pub fn pre(self) -> RString ⓘ
Returns the string before the segment matched.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new("b(.)r", Default::default()).unwrap();
regexp.reg_match("foo bar baz").unwrap();
let match_data = backref_get().unwrap();
assert_eq!(match_data.pre().to_string().unwrap(), "foo ");
sourcepub fn post(self) -> RString ⓘ
pub fn post(self) -> RString ⓘ
Returns the string after the segment matched.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new("b(.)r", Default::default()).unwrap();
regexp.reg_match("foo bar baz").unwrap();
let match_data = backref_get().unwrap();
assert_eq!(match_data.post().to_string().unwrap(), " baz");
sourcepub fn last(self) -> Option<RString>
pub fn last(self) -> Option<RString>
Returns the last capture.
Returns None
if there are no capture groups.
§Examples
use magnus::{backref_get, RRegexp};
let regexp = RRegexp::new("(.)oo b(.)r ba(.)", Default::default()).unwrap();
regexp.reg_match("foo bar baz").unwrap();
let match_data = backref_get().unwrap();
assert_eq!(match_data.last().unwrap().to_string().unwrap(), "z");
Trait Implementations§
source§impl Object for RMatch
impl Object for RMatch
source§fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
self
’s scope. Read moresource§impl ReprValue for RMatch
impl ReprValue for RMatch
source§fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
#==
. Read moresource§fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
#eql?
. Read moresource§fn hash(self) -> Result<Integer, Error>
fn hash(self) -> Result<Integer, Error>
self
. Read moresource§fn to_bool(self) -> bool
fn to_bool(self) -> bool
self
to a bool
, following Ruby’s rules of false
and nil
as boolean false
and everything else boolean true
. Read moresource§fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
source§fn block_call<M, A, R, T>(
self,
method: M,
args: A,
block: fn(_: &[Value], _: Option<Proc>) -> R
) -> Result<T, Error>
fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
source§fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
self
responds to the given Ruby method. Read more