more examples - particularly multiple captures
This commit is contained in:
parent
7697b9ffb3
commit
bd92afd0df
26
examples/errors.rs
Normal file
26
examples/errors.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
extern crate lua_patterns;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let bad = [
|
||||||
|
("bonzo %","malformed pattern (ends with '%')"),
|
||||||
|
("bonzo (dog%(","unfinished capture"),
|
||||||
|
("alles [%a%[","malformed pattern (missing ']')"),
|
||||||
|
("bonzo (dog (cat)","unfinished capture"),
|
||||||
|
("frodo %f[%A","malformed pattern (missing ']')"),
|
||||||
|
("frodo (1) (2(3)%2)%1","invalid capture index %2"),
|
||||||
|
];
|
||||||
|
|
||||||
|
fn error(s: &str) -> lua_patterns::PatternError {
|
||||||
|
lua_patterns::PatternError(s.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
for p in bad.iter() {
|
||||||
|
let res = lua_patterns::LuaPattern::new_try(p.0);
|
||||||
|
if let Err(e) = res {
|
||||||
|
assert_eq!(e, error(p.1));
|
||||||
|
} else {
|
||||||
|
println!("'{}' was fine",p.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
examples/multiple_captures.rs
Normal file
9
examples/multiple_captures.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
extern crate lua_patterns as lp;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut p = lp::LuaPattern::new("%s*(%d+)%s+(%S+)");
|
||||||
|
if let Some((int,rest)) = p.match_maybe_2(" 233 hello dolly") {
|
||||||
|
assert_eq!(int,"233");
|
||||||
|
assert_eq!(rest,"hello");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue