1
1
regex
2
2
=====
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
9
8
by [ RE2] ( https://github.com/google/re2 ) .
10
9
11
10
[ ![ 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`:
29
28
30
29
``` toml
31
30
[dependencies ]
32
- regex = " 0.1 "
31
+ regex = " 0.2 "
33
32
```
34
33
35
34
and this to your crate root:
@@ -56,9 +55,9 @@ fn main() {
56
55
" ). unwrap ();
57
56
let caps = re . captures (" 2010-03-14" ). unwrap ();
58
57
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" ] );
62
61
}
63
62
```
64
63
@@ -82,9 +81,9 @@ fn main() {
82
81
// because the only way for the regex to match is if all of the
83
82
// capture groups match. This is not true in general though!
84
83
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 ());
88
87
}
89
88
}
90
89
```
@@ -137,8 +136,8 @@ means the main API can't be used for searching arbitrary bytes.
137
136
To match on arbitrary bytes, use the ` regex::bytes::Regex ` API. The API
138
137
is identical to the main API, except that it takes an ` &[u8] ` to search
139
138
on 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.
142
141
143
142
This example shows how to find all null-terminated strings in a slice of bytes:
144
143
@@ -152,7 +151,7 @@ let text = b"foo\x00bar\x00baz\x00";
152
151
// The unwrap is OK here since a match requires the `cstr` capture to match.
153
152
let cstrs : Vec <& [u8 ]> =
154
153
re . captures_iter (text )
155
- . map (| c | c . name (" cstr" ). unwrap ())
154
+ . map (| c | c . name (" cstr" ). unwrap (). as_bytes () )
156
155
. collect ();
157
156
assert_eq! (vec! [& b " foo" [.. ], & b " bar" [.. ], & b " baz" [.. ]], cstrs );
158
157
```
@@ -211,9 +210,9 @@ fn main() {
211
210
let re = regex! (r " (\d{4})-(\d{2})-(\d{2})" );
212
211
let caps = re . captures (" 2010-03-14" ). unwrap ();
213
212
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 ] );
217
216
}
218
217
```
219
218
0 commit comments