mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 03:23:05 +00:00
Merge pull request #387 from skierpage/patch-1
rephrase text pattern replacement example
This commit is contained in:
commit
8c15ed7c7f
1 changed files with 6 additions and 6 deletions
|
@ -825,8 +825,8 @@ fn main() {
|
||||||
|
|
||||||
[![regex-badge]][regex] [![lazy_static-badge]][lazy_static] [![cat-text-processing-badge]][cat-text-processing]
|
[![regex-badge]][regex] [![lazy_static-badge]][lazy_static] [![cat-text-processing-badge]][cat-text-processing]
|
||||||
|
|
||||||
Replaces all occurrences of the hyphenated British English date pattern `2013-01-15`
|
Replaces all occurrences of the standard ISO 8601 *YYYY-MM-DD* date pattern
|
||||||
with its equivalent slashed American English date pattern `01/15/2013`.
|
with the equivalent American English date with slashes; for example `2013-01-15` becomes `01/15/2013`.
|
||||||
|
|
||||||
The method [`Regex::replace_all`] replaces all occurrences of the whole regex. The
|
The method [`Regex::replace_all`] replaces all occurrences of the whole regex. The
|
||||||
`Replacer` trait helps to figure out the replacement string. This trait is implemented
|
`Replacer` trait helps to figure out the replacement string. This trait is implemented
|
||||||
|
@ -844,17 +844,17 @@ use regex::Regex;
|
||||||
|
|
||||||
fn reformat_dates(before: &str) -> Cow<str> {
|
fn reformat_dates(before: &str) -> Cow<str> {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref ENGL_DATE_REGEX : Regex = Regex::new(
|
static ref ISO8601_DATE_REGEX : Regex = Regex::new(
|
||||||
r"(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})"
|
r"(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})"
|
||||||
).unwrap();
|
).unwrap();
|
||||||
}
|
}
|
||||||
ENGL_DATE_REGEX.replace_all(before, "$m/$d/$y")
|
ISO8601_DATE_REGEX.replace_all(before, "$m/$d/$y")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let before = "2012-03-14, 2013-01-01 and 2014-07-05";
|
let before = "2012-03-14, 2013-01-15 and 2014-07-05";
|
||||||
let after = reformat_dates(before);
|
let after = reformat_dates(before);
|
||||||
assert_eq!(after, "03/14/2012, 01/01/2013 and 07/05/2014");
|
assert_eq!(after, "03/14/2012, 01/15/2013 and 07/05/2014");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue