| 1 |
macro_rules! replace( |
| 2 |
($name:ident, $which:ident, $re:expr, |
| 3 |
$search:expr, $replace:expr, $result:expr) => ( |
| 4 |
#[test] |
| 5 |
fn $name() { |
| 6 |
let re = regex::Regex::new($re).unwrap(); |
| 7 |
assert_eq!(re.$which($search, $replace), $result); |
| 8 |
} |
| 9 |
); |
| 10 |
); |
| 11 |
|
| 12 |
replace!(first, replace, r"[0-9]", "age: 26", "Z", "age: Z6"); |
| 13 |
replace!(plus, replace, r"[0-9]+", "age: 26", "Z", "age: Z"); |
| 14 |
replace!(all, replace_all, r"[0-9]", "age: 26", "Z", "age: ZZ"); |
| 15 |
replace!(groups, replace, r"([^ ]+)[ ]+([^ ]+)", "w1 w2", "$2 $1", "w2 w1"); |
| 16 |
replace!( |
| 17 |
double_dollar, |
| 18 |
replace, |
| 19 |
r"([^ ]+)[ ]+([^ ]+)", |
| 20 |
"w1 w2", |
| 21 |
"$2 $$1", |
| 22 |
"w2 $1" |
| 23 |
); |
| 24 |
// replace!(adjacent_index, replace, |
| 25 |
// r"([^aeiouy])ies$", "skies", "$1y", "sky"); |
| 26 |
replace!( |
| 27 |
named, |
| 28 |
replace_all, |
| 29 |
r"(?P<first>[^ ]+)[ ]+(?P<last>[^ ]+)(?P<space>[ ]*)", |
| 30 |
"w1 w2 w3 w4", |
| 31 |
"$last $first$space", |
| 32 |
"w2 w1 w4 w3" |
| 33 |
); |
| 34 |
replace!( |
| 35 |
trim, |
| 36 |
replace_all, |
| 37 |
"^[ \t]+|[ \t]+$", |
| 38 |
" \t trim me\t \t", |
| 39 |
"", |
| 40 |
"trim me" |
| 41 |
); |
| 42 |
replace!(number_hyphen, replace, r"(.)(.)", "ab", "$1-$2", "a-b"); |
| 43 |
// replace!(number_underscore, replace, r"(.)(.)", "ab", "$1_$2", "a_b"); |
| 44 |
replace!( |
| 45 |
simple_expand, |
| 46 |
replace_all, |
| 47 |
r"([a-z]) ([a-z])", |
| 48 |
"a b", |
| 49 |
"$2 $1", |
| 50 |
"b a" |
| 51 |
); |
| 52 |
replace!( |
| 53 |
literal_dollar1, |
| 54 |
replace_all, |
| 55 |
r"([a-z]+) ([a-z]+)", |
| 56 |
"a b", |
| 57 |
"$$1", |
| 58 |
"$1" |
| 59 |
); |
| 60 |
replace!( |
| 61 |
literal_dollar2, |
| 62 |
replace_all, |
| 63 |
r"([a-z]+) ([a-z]+)", |
| 64 |
"a b", |
| 65 |
"$2 $$c $1", |
| 66 |
"b $c a" |
| 67 |
); |
| 68 |
replace!( |
| 69 |
no_expand1, |
| 70 |
replace, |
| 71 |
r"([^ ]+)[ ]+([^ ]+)", |
| 72 |
"w1 w2", |
| 73 |
regex::NoExpand("$2 $1"), |
| 74 |
"$2 $1" |
| 75 |
); |
| 76 |
replace!( |
| 77 |
no_expand2, |
| 78 |
replace, |
| 79 |
r"([^ ]+)[ ]+([^ ]+)", |
| 80 |
"w1 w2", |
| 81 |
regex::NoExpand("$$1"), |
| 82 |
"$$1" |
| 83 |
); |
| 84 |
replace!( |
| 85 |
closure_returning_reference, |
| 86 |
replace, |
| 87 |
r"([0-9]+)", |
| 88 |
"age: 26", |
| 89 |
|captures: ®ex::Captures<'_>| { captures[1][0..1].to_owned() }, |
| 90 |
"age: 2" |
| 91 |
); |
| 92 |
replace!( |
| 93 |
closure_returning_value, |
| 94 |
replace, |
| 95 |
r"[0-9]+", |
| 96 |
"age: 26", |
| 97 |
|_captures: ®ex::Captures<'_>| "Z".to_owned(), |
| 98 |
"age: Z" |
| 99 |
); |
| 100 |
|
| 101 |
// See https://github.com/rust-lang/regex/issues/314 |
| 102 |
replace!( |
| 103 |
match_at_start_replace_with_empty, |
| 104 |
replace_all, |
| 105 |
r"foo", |
| 106 |
"foobar", |
| 107 |
"", |
| 108 |
"bar" |
| 109 |
); |
| 110 |
|
| 111 |
// See https://github.com/rust-lang/regex/issues/393 |
| 112 |
replace!(single_empty_match, replace, r"^", "bar", "foo", "foobar"); |
| 113 |
|
| 114 |
// See https://github.com/rust-lang/regex/issues/399 |
| 115 |
replace!( |
| 116 |
capture_longest_possible_name, |
| 117 |
replace_all, |
| 118 |
r"(.)", |
| 119 |
"b", |
| 120 |
"${1}a $1a", |
| 121 |
"ba " |
| 122 |
); |
| 123 |
|
| 124 |
replace!( |
| 125 |
impl_string, |
| 126 |
replace, |
| 127 |
r"[0-9]", |
| 128 |
"age: 26", |
| 129 |
"Z".to_string(), |
| 130 |
"age: Z6" |
| 131 |
); |
| 132 |
replace!( |
| 133 |
impl_string_ref, |
| 134 |
replace, |
| 135 |
r"[0-9]", |
| 136 |
"age: 26", |
| 137 |
&"Z".to_string(), |
| 138 |
"age: Z6" |
| 139 |
); |
| 140 |
replace!( |
| 141 |
impl_cow_str_borrowed, |
| 142 |
replace, |
| 143 |
r"[0-9]", |
| 144 |
"age: 26", |
| 145 |
std::borrow::Cow::<'_, str>::Borrowed("Z"), |
| 146 |
"age: Z6" |
| 147 |
); |
| 148 |
replace!( |
| 149 |
impl_cow_str_borrowed_ref, |
| 150 |
replace, |
| 151 |
r"[0-9]", |
| 152 |
"age: 26", |
| 153 |
&std::borrow::Cow::<'_, str>::Borrowed("Z"), |
| 154 |
"age: Z6" |
| 155 |
); |
| 156 |
replace!( |
| 157 |
impl_cow_str_owned, |
| 158 |
replace, |
| 159 |
r"[0-9]", |
| 160 |
"age: 26", |
| 161 |
std::borrow::Cow::<'_, str>::Owned("Z".to_string()), |
| 162 |
"age: Z6" |
| 163 |
); |
| 164 |
replace!( |
| 165 |
impl_cow_str_owned_ref, |
| 166 |
replace, |
| 167 |
r"[0-9]", |
| 168 |
"age: 26", |
| 169 |
&std::borrow::Cow::<'_, str>::Owned("Z".to_string()), |
| 170 |
"age: Z6" |
| 171 |
); |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn replacen_no_captures() { |
| 175 |
let re = regex::Regex::new(r"[0-9]").unwrap(); |
| 176 |
assert_eq!(re.replacen("age: 1234", 2, "Z"), "age: ZZ34"); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn replacen_with_captures() { |
| 181 |
let re = regex::Regex::new(r"([0-9])").unwrap(); |
| 182 |
assert_eq!(re.replacen("age: 1234", 2, "${1}Z"), "age: 1Z2Z34"); |
| 183 |
} |