Skip to content

Commit 3a6138b

Browse files
committed
Add another replacement string example.
This shows how to use curly braces in the replacement string, and more specifically, explains why they are sometimes necessary. Fixes #333
1 parent 767f80f commit 3a6138b

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/re_bytes.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,23 @@ impl Regex {
427427
/// Note that using `$2` instead of `$first` or `$1` instead of `$last`
428428
/// would produce the same result. To write a literal `$` use `$$`.
429429
///
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:
432434
///
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.
436447
///
437448
/// Finally, sometimes you just want to replace a literal string with no
438449
/// regard for capturing group expansion. This can be done by wrapping a

src/re_unicode.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,24 @@ impl Regex {
501501
/// Note that using `$2` instead of `$first` or `$1` instead of `$last`
502502
/// would produce the same result. To write a literal `$` use `$$`.
503503
///
504+
/// Sometimes the replacement string requires use of curly braces to
505+
/// delineate a capture group replacement and surrounding literal text.
506+
/// For example, if we wanted to join two words together with an
507+
/// underscore:
508+
///
509+
/// ```rust
510+
/// # extern crate regex; use regex::Regex;
511+
/// # fn main() {
512+
/// let re = Regex::new(r"(?P<first>\w+)\s+(?P<second>\w+)").unwrap();
513+
/// let result = re.replace("deep fried", "${first}_$second");
514+
/// assert_eq!(result, "deep_fried");
515+
/// # }
516+
/// ```
517+
///
518+
/// Without the curly braces, the capture group name `first_` would be
519+
/// used, and since it doesn't exist, it would be replaced with the empty
520+
/// string.
521+
///
504522
/// Finally, sometimes you just want to replace a literal string with no
505523
/// regard for capturing group expansion. This can be done by wrapping a
506524
/// byte string with `NoExpand`:

0 commit comments

Comments
 (0)