Skip to content

Commit

Permalink
Add another replacement string example.
Browse files Browse the repository at this point in the history
This shows how to use curly braces in the replacement string, and more
specifically, explains why they are sometimes necessary.

Fixes rust-lang#333
  • Loading branch information
BurntSushi committed Feb 18, 2017
1 parent 767f80f commit 3a6138b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,23 @@ impl Regex {
/// Note that using `$2` instead of `$first` or `$1` instead of `$last`
/// would produce the same result. To write a literal `$` use `$$`.
///
/// If `$name` isn't a valid capture group (whether the name doesn't exist
/// or isn't a valid index), then it is replaced with the empty string.
/// Sometimes the replacement string requires use of curly braces to
/// delineate a capture group replacement and surrounding literal text.
/// For example, if we wanted to join two words together with an
/// underscore:
///
/// The longest possible name is used. e.g., `$1a` looks up the capture
/// group named `1a` and not the capture group at index `1`. To exert more
/// precise control over the name, use braces, e.g., `${1}a`.
/// ```rust
/// # extern crate regex; use regex::bytes::Regex;
/// # fn main() {
/// let re = Regex::new(r"(?P<first>\w+)\s+(?P<second>\w+)").unwrap();
/// let result = re.replace(b"deep fried", &b"${first}_$second"[..]);
/// assert_eq!(result, &b"deep_fried"[..]);
/// # }
/// ```
///
/// Without the curly braces, the capture group name `first_` would be
/// used, and since it doesn't exist, it would be replaced with the empty
/// string.
///
/// Finally, sometimes you just want to replace a literal string with no
/// regard for capturing group expansion. This can be done by wrapping a
Expand Down
18 changes: 18 additions & 0 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,24 @@ impl Regex {
/// Note that using `$2` instead of `$first` or `$1` instead of `$last`
/// would produce the same result. To write a literal `$` use `$$`.
///
/// Sometimes the replacement string requires use of curly braces to
/// delineate a capture group replacement and surrounding literal text.
/// For example, if we wanted to join two words together with an
/// underscore:
///
/// ```rust
/// # extern crate regex; use regex::Regex;
/// # fn main() {
/// let re = Regex::new(r"(?P<first>\w+)\s+(?P<second>\w+)").unwrap();
/// let result = re.replace("deep fried", "${first}_$second");
/// assert_eq!(result, "deep_fried");
/// # }
/// ```
///
/// Without the curly braces, the capture group name `first_` would be
/// used, and since it doesn't exist, it would be replaced with the empty
/// string.
///
/// Finally, sometimes you just want to replace a literal string with no
/// regard for capturing group expansion. This can be done by wrapping a
/// byte string with `NoExpand`:
Expand Down

0 comments on commit 3a6138b

Please sign in to comment.