@@ -427,12 +427,23 @@ impl Regex {
427
427
/// Note that using `$2` instead of `$first` or `$1` instead of `$last`
428
428
/// would produce the same result. To write a literal `$` use `$$`.
429
429
///
430
- /// If `$name` isn't a valid capture group (whether the name doesn't exist
431
- /// or isn't a valid index), then it is replaced with the empty string.
430
+ /// Sometimes the replacement string requires use of curly braces to
431
+ /// delineate a capture group replacement and surrounding literal text.
432
+ /// For example, if we wanted to join two words together with an
433
+ /// underscore:
432
434
///
433
- /// The longest possible name is used. e.g., `$1a` looks up the capture
434
- /// group named `1a` and not the capture group at index `1`. To exert more
435
- /// precise control over the name, use braces, e.g., `${1}a`.
435
+ /// ```rust
436
+ /// # extern crate regex; use regex::bytes::Regex;
437
+ /// # fn main() {
438
+ /// let re = Regex::new(r"(?P<first>\w+)\s+(?P<second>\w+)").unwrap();
439
+ /// let result = re.replace(b"deep fried", &b"${first}_$second"[..]);
440
+ /// assert_eq!(result, &b"deep_fried"[..]);
441
+ /// # }
442
+ /// ```
443
+ ///
444
+ /// Without the curly braces, the capture group name `first_` would be
445
+ /// used, and since it doesn't exist, it would be replaced with the empty
446
+ /// string.
436
447
///
437
448
/// Finally, sometimes you just want to replace a literal string with no
438
449
/// regard for capturing group expansion. This can be done by wrapping a
0 commit comments