11regex
22=====
3-
4- A Rust library for parsing, compiling, and executing regular expressions.
5- This particular implementation of regular expressions guarantees execution
6- in linear time with respect to the size of the regular expression and
7- search text by using finite automata. In particular, it makes use of both
8- NFAs and DFAs when matching. Much of the syntax and implementation is inspired
3+ A Rust library for parsing, compiling, and executing regular expressions. Its
4+ syntax is similar to Perl-style regular expressions, but lacks a few features
5+ like look around and backreferences. In exchange, all searches execute in
6+ linear time with respect to the size of the regular expression and search text.
7+ Much of the syntax and implementation is inspired
98by [ RE2] ( https://github.com/google/re2 ) .
109
1110[ ![ Build Status] ( https://travis-ci.org/rust-lang-nursery/regex.svg?branch=master )] ( https://travis-ci.org/rust-lang-nursery/regex )
@@ -29,7 +28,7 @@ Add this to your `Cargo.toml`:
2928
3029``` toml
3130[dependencies ]
32- regex = " 0.1 "
31+ regex = " 0.2 "
3332```
3433
3534and this to your crate root:
@@ -56,9 +55,9 @@ fn main() {
5655" ). unwrap ();
5756 let caps = re . captures (" 2010-03-14" ). unwrap ();
5857
59- assert_eq! (" 2010" , caps . name ( " year" ) . unwrap () );
60- assert_eq! (" 03" , caps . name ( " month" ) . unwrap () );
61- assert_eq! (" 14" , caps . name ( " day" ) . unwrap () );
58+ assert_eq! (" 2010" , caps [ " year" ] );
59+ assert_eq! (" 03" , caps [ " month" ] );
60+ assert_eq! (" 14" , caps [ " day" ] );
6261}
6362```
6463
@@ -82,9 +81,9 @@ fn main() {
8281 // because the only way for the regex to match is if all of the
8382 // capture groups match. This is not true in general though!
8483 println! (" year: {}, month: {}, day: {}" ,
85- caps . at (1 ). unwrap (),
86- caps . at (2 ). unwrap (),
87- caps . at (3 ). unwrap ());
84+ caps . get (1 ). unwrap () . as_str (),
85+ caps . get (2 ). unwrap () . as_str (),
86+ caps . get (3 ). unwrap () . as_str ());
8887 }
8988}
9089```
@@ -137,8 +136,8 @@ means the main API can't be used for searching arbitrary bytes.
137136To match on arbitrary bytes, use the ` regex::bytes::Regex ` API. The API
138137is identical to the main API, except that it takes an ` &[u8] ` to search
139138on instead of an ` &str ` . By default, ` . ` will match any * byte* using
140- ` regex::bytes::Regex ` , while ` . ` will match any encoded Unicode * codepoint *
141- using the main API.
139+ ` regex::bytes::Regex ` , while ` . ` will match any * UTF-8 encoded Unicode scalar
140+ value * using the main API.
142141
143142This example shows how to find all null-terminated strings in a slice of bytes:
144143
@@ -152,7 +151,7 @@ let text = b"foo\x00bar\x00baz\x00";
152151// The unwrap is OK here since a match requires the `cstr` capture to match.
153152let cstrs : Vec <& [u8 ]> =
154153 re . captures_iter (text )
155- . map (| c | c . name (" cstr" ). unwrap ())
154+ . map (| c | c . name (" cstr" ). unwrap (). as_bytes () )
156155 . collect ();
157156assert_eq! (vec! [& b " foo" [.. ], & b " bar" [.. ], & b " baz" [.. ]], cstrs );
158157```
@@ -211,9 +210,9 @@ fn main() {
211210 let re = regex! (r " (\d{4})-(\d{2})-(\d{2})" );
212211 let caps = re . captures (" 2010-03-14" ). unwrap ();
213212
214- assert_eq! (" 2010" , caps . at ( 1 ) . unwrap () );
215- assert_eq! (" 03" , caps . at ( 2 ) . unwrap () );
216- assert_eq! (" 14" , caps . at ( 3 ) . unwrap () );
213+ assert_eq! (" 2010" , caps [ 1 ] );
214+ assert_eq! (" 03" , caps [ 2 ] );
215+ assert_eq! (" 14" , caps [ 3 ] );
217216}
218217```
219218
0 commit comments