let mut m = lua_patterns::LuaPattern::new("one"); let text = "hello one two"; assert!(m.matches(text)); let r = m.range(); assert_eq!(r.start, 6); assert_eq!(r.end, 9); --- let mut m = lua_patterns::LuaPattern::new("(%a+) one"); let text = " hello one two"; assert!(m.matches(text)); assert_eq!(m.capture(0),1..10); // "hello one" assert_eq!(m.capture(1),1..6); // "hello" --- let mut m = lua_patterns::LuaPattern::new("OK%s+(%d+)"); let res = m.match_maybe("and that's OK 400 to you"); assert_eq!(res, Some("400")); --- let mut m = lua_patterns::LuaPattern::new("(%a+) one"); let text = " hello one two"; let v = m.captures(text); assert_eq!(v, &["hello one","hello"]); --- let mut m = lua_patterns::LuaPattern::new("%S+"); let split: Vec<_> = m.gmatch("dog cat leopard wolf ").collect(); assert_eq!(split,&["dog","cat","leopard","wolf"]); --- let mut m = lua_patterns::LuaPattern::new("(%S)%S+"); let split: Vec<_> = m.gmatch_captures("dog cat leopard wolf") .map(|cc| cc.get(1)).collect(); assert_eq!(split,&["d","c","l","w"]); --- let mut m = lua_patterns::LuaPattern::new("%$(%S+)"); let res = m.gsub_with("hello $dolly you're so $fine", |cc| cc.get(1).to_uppercase() ); assert_eq!(res,"hello DOLLY you're so FINE"); --- let mut m = lua_patterns::LuaPattern::new("%s+"); let res = m.gsub("hello dolly you're so fine",""); assert_eq!(res, "hellodollyyou'resofine"); let mut m = lua_patterns::LuaPattern::new("(%S+)%s*=%s*(%S+);%s*"); let res = m.gsub("a=2; b=3; c = 4;", "'%2':%1 "); assert_eq!(res, "'2':a '3':b '4':c "); --- use std::collections::HashMap; let mut map = HashMap::new(); // updating old lines for the 21st Century map.insert("dolly", "baby"); map.insert("fine", "cool"); map.insert("good-looking", "pretty"); let mut m = lua_patterns::LuaPattern::new("%$%((.-)%)"); let res = m.gsub_with("hello $(dolly) you're so $(fine) and $(good-looking)", |cc| map.get(cc.get(1)).unwrap_or(&"?").to_string() ); assert_eq!(res,"hello baby you're so cool and pretty"); --- let mut m = lua_patterns::LuaPattern::new("(%S+)%s*=%s*([^;]+);"); let res = m.gsub_with("alpha=bonzo; beta=felix;", |cc| format!("{}:'{}',", cc.get(1), cc.get(2)) ); assert_eq!(res, "alpha:'bonzo', beta:'felix',"); --- let mut words = lua_patterns::LuaPattern::new("%a%a+"); for w in words.gmatch_bytes(b"hello dolly") { println!("{}",String::from_utf8_lossy(w)); } --- let patt = &[0xDE,0x00,b'+',0xBE]; let bytes = &[0xFF,0xEE,0x0,0xDE,0x0,0x0,0xBE,0x0,0x0]; let mut m = lua_patterns::LuaPattern::from_bytes(patt); assert!(m.matches_bytes(bytes)); assert_eq!(&bytes[m.capture(0)], &[0xDE,0x00,0x00,0xBE]); --- let bytes = &[0xFF,0xEE,0x0,0xDE,0x24,0x24,0xBE,0x0,0x0]; let patt = lua_patterns::LuaPatternBuilder::new() .bytes_as_hex("DE24") // less tedious than a byte slice .text("+") // unescaped .bytes(&[0xBE]) // byte slice .build(); let mut m = lua_patterns::LuaPattern::from_bytes(&patt); // picks up "DE2424BE" --- let mut m = lua_patterns::LuaPattern::new("(%a+) one"); let text = " hello one two"; let mut v = Vec::new(); if m.capture_into(text,&mut v) { assert_eq!(v, &["hello one","hello"]); } ---