|
43 | 43 |
|
44 | 44 | (defn replace
|
45 | 45 | "Replaces all instance of match with replacement in s.
|
| 46 | +
|
46 | 47 | match/replacement can be:
|
47 | 48 |
|
48 | 49 | string / string
|
49 |
| - pattern / (string or function of match)." |
| 50 | + pattern / (string or function of match). |
| 51 | +
|
| 52 | + See also replace-first. |
| 53 | +
|
| 54 | + The replacement is literal (i.e. none of its characters are treated |
| 55 | + specially) for all cases above except pattern / string. |
| 56 | +
|
| 57 | + For pattern / string, $1, $2, etc. in the replacement string are |
| 58 | + substituted with the string that matched the corresponding |
| 59 | + parenthesized group in the pattern. |
| 60 | +
|
| 61 | + Example: |
| 62 | + (clojure.string/replace \"Almost Pig Latin\" #\"\\b(\\w)(\\w+)\\b\" \"$2$1ay\") |
| 63 | + -> \"lmostAay igPay atinLay\"" |
50 | 64 | [s match replacement]
|
51 | 65 | (cond
|
52 | 66 | (string? match)
|
|
61 | 75 |
|
62 | 76 | (defn replace-first
|
63 | 77 | "Replaces the first instance of match with replacement in s.
|
| 78 | +
|
64 | 79 | match/replacement can be:
|
65 | 80 |
|
66 | 81 | string / string
|
67 |
| - pattern / (string or function of match)." |
| 82 | + pattern / (string or function of match). |
| 83 | +
|
| 84 | + See also replace. |
| 85 | +
|
| 86 | + The replacement is literal (i.e. none of its characters are treated |
| 87 | + specially) for all cases above except pattern / string. |
| 88 | +
|
| 89 | + For pattern / string, $1, $2, etc. in the replacement string are |
| 90 | + substituted with the string that matched the corresponding |
| 91 | + parenthesized group in the pattern. |
| 92 | +
|
| 93 | + Example: |
| 94 | + (clojure.string/replace-first \"swap first two words\" |
| 95 | + #\"(\\w+)(\\s+)(\\w+)\" \"$3$2$1\") |
| 96 | + -> \"first swap two words\"" |
68 | 97 | [s match replacement]
|
69 | 98 | (.replace s match replacement))
|
70 | 99 |
|
|
0 commit comments