0001    extension String {
0002    
0003      // MARK: Replacing the first match (mutating)
0004    
0005      /// If `regex` matches at least one substring, replace the first match with
0006      /// `template`.
0007      ///
0008      /// The template string may be a literal string, or include template variables:
0009      /// the variable `$0` will be replaced with the entire matched substring, `$1`
0010      /// with the first capture group, etc.
0011      ///
0012      /// For example, to include the literal string "$1" in the replacement string,
0013      /// you must escape the "$": `\$1`.
0014      ///
0015      /// - parameters:
0016      ///     - regex: A regular expression to match against `self`.
0017      ///     - template: A template string used to replace matches.
0018      public mutating func replaceFirstMatching
String+ReplaceMatching.swift:50
    replaceFirstMatching(Regex(pattern), with: template)
String+ReplaceMatching.swift:74
    string.replaceFirstMatching(regex, with: template)
(regex: Regex, with template: String) { 0019 if let match = regex.match(self) { 0020 let replacement = regex 0021 .regularExpression 0022 .replacementStringForResult(match.matchResult, 0023 inString: self, 0024 offset: 0, 0025 template: template) 0026 0027 replaceRange(match.range, with: replacement) 0028 } 0029 } 0030 0031 /// If the regular expression described by `pattern` matches at least one 0032 /// substring, replace the first match with `template`. 0033 /// 0034 /// Convenience overload that accepts a `StaticString` instead of a `Regex`. 0035 /// 0036 /// The template string may be a literal string, or include template variables: 0037 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0038 /// with the first capture group, etc. 0039 /// 0040 /// For example, to include the literal string "$1" in the replacement string, 0041 /// you must escape the "$": `\$1`. 0042 /// 0043 /// - requires: `pattern` is a valid regular expression. Invalid regular 0044 /// expressions will cause this method to trap. 0045 /// 0046 /// - parameters: 0047 /// - pattern: A regular expression pattern to match against `self`. 0048 /// - template: A template string used to replace matches. 0049 public mutating func replaceFirstMatching(pattern: StaticString, with template: String) { 0050 replaceFirstMatching(Regex(pattern), with: template) 0051 } 0052 0053 0054 0055 // MARK: Replacing the first match (nonmutating) 0056 0057 /// Returns a new string where the first match of `regex` is replaced with 0058 /// `template`. 0059 /// 0060 /// The template string may be a literal string, or include template variables: 0061 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0062 /// with the first capture group, etc. 0063 /// 0064 /// For example, to include the literal string "$1" in the replacement string, 0065 /// you must escape the "$": `\$1`. 0066 /// 0067 /// - parameters: 0068 /// - regex: A regular expression to match against `self`. 0069 /// - template: A template string used to replace matches. 0070 /// 0071 /// - returns: A string with the first match of `regex` replaced by `template`. 0072 public func replacingFirstMatching
String+ReplaceMatching.swift:99
    return replacingFirstMatching(Regex(pattern), with: template)
(regex: Regex, with template: String) -> String { 0073 var string = self 0074 string.replaceFirstMatching(regex, with: template) 0075 return string 0076 } 0077 0078 /// Returns a new string where the first match of the regular expression 0079 /// described by `pattern` is replaced with `template`. 0080 /// 0081 /// Convenience overload that accepts a `StaticString` instead of a `Regex`. 0082 /// 0083 /// The template string may be a literal string, or include template variables: 0084 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0085 /// with the first capture group, etc. 0086 /// 0087 /// For example, to include the literal string "$1" in the replacement string, 0088 /// you must escape the "$": `\$1`. 0089 /// 0090 /// - requires: `pattern` is a valid regular expression. Invalid regular 0091 /// expressions will cause this method to trap. 0092 /// 0093 /// - parameters: 0094 /// - pattern: A regular expression pattern to match against `self`. 0095 /// - template: A template string used to replace matches. 0096 /// 0097 /// - returns: A string with the first match of `pattern` replaced by `template`. 0098 public func replacingFirstMatching(pattern: StaticString, with template: String) -> String { 0099 return replacingFirstMatching(Regex(pattern), with: template) 0100 } 0101 0102 0103 0104 // MARK: Replacing all matches (mutating) 0105 0106 /// Replace each substring matched by `regex` with `template`. 0107 /// 0108 /// The template string may be a literal string, or include template variables: 0109 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0110 /// with the first capture group, etc. 0111 /// 0112 /// For example, to include the literal string "$1" in the replacement string, 0113 /// you must escape the "$": `\$1`. 0114 /// 0115 /// - parameters: 0116 /// - regex: A regular expression to match against `self`. 0117 /// - template: A template string used to replace matches. 0118 public mutating func replaceAllMatching
String+ReplaceMatching.swift:150
    replaceAllMatching(Regex(pattern), with: template)
String+ReplaceMatching.swift:174
    string.replaceAllMatching(regex, with: template)
(regex: Regex, with template: String) { 0119 for match in regex.allMatches(self).reverse() { 0120 let replacement = regex 0121 .regularExpression 0122 .replacementStringForResult(match.matchResult, 0123 inString: self, 0124 offset: 0, 0125 template: template) 0126 0127 replaceRange(match.range, with: replacement) 0128 } 0129 } 0130 0131 /// Replace each substring matched by the regular expression described in 0132 /// `pattern` with `template`. 0133 /// 0134 /// Convenience overload that accepts a `StaticString` instead of a `Regex`. 0135 /// 0136 /// The template string may be a literal string, or include template variables: 0137 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0138 /// with the first capture group, etc. 0139 /// 0140 /// For example, to include the literal string "$1" in the replacement string, 0141 /// you must escape the "$": `\$1`. 0142 /// 0143 /// - requires: `pattern` is a valid regular expression. Invalid regular 0144 /// expressions will cause this method to trap. 0145 /// 0146 /// - parameters: 0147 /// - pattern: A regular expression pattern to match against `self`. 0148 /// - template: A template string used to replace matches. 0149 public mutating func replaceAllMatching(pattern: StaticString, with template: String) { 0150 replaceAllMatching(Regex(pattern), with: template) 0151 } 0152 0153 0154 0155 // MARK: Replacing all matches (nonmutating) 0156 0157 /// Returns a new string where each substring matched by `regex` is replaced 0158 /// with `template`. 0159 /// 0160 /// The template string may be a literal string, or include template variables: 0161 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0162 /// with the first capture group, etc. 0163 /// 0164 /// For example, to include the literal string "$1" in the replacement string, 0165 /// you must escape the "$": `\$1`. 0166 /// 0167 /// - parameters: 0168 /// - regex: A regular expression to match against `self`. 0169 /// - template: A template string used to replace matches. 0170 /// 0171 /// - returns: A string with all matches of `regex` replaced by `template`. 0172 public func replacingAllMatching
String+ReplaceMatching.swift:199
    return replacingAllMatching(Regex(pattern), with: template)
(regex: Regex, with template: String) -> String { 0173 var string = self 0174 string.replaceAllMatching(regex, with: template) 0175 return string 0176 } 0177 0178 /// Returns a new string where each substring matched by the regular 0179 /// expression described in `pattern` is replaced with `template`. 0180 /// 0181 /// Convenience overload that accepts a `StaticString` instead of a `Regex`. 0182 /// 0183 /// The template string may be a literal string, or include template variables: 0184 /// the variable `$0` will be replaced with the entire matched substring, `$1` 0185 /// with the first capture group, etc. 0186 /// 0187 /// For example, to include the literal string "$1" in the replacement string, 0188 /// you must escape the "$": `\$1`. 0189 /// 0190 /// - requires: `pattern` is a valid regular expression. Invalid regular 0191 /// expressions will cause this method to trap. 0192 /// 0193 /// - parameters: 0194 /// - pattern: A regular expression pattern to match against `self`. 0195 /// - template: A template string used to replace matches. 0196 /// 0197 /// - returns: A string with all matches of `pattern` replaced by `template`. 0198 public func replacingAllMatching(pattern: StaticString, with template: String) -> String { 0199 return replacingAllMatching(Regex(pattern), with: template) 0200 } 0201 0202 } 0203 0204 import Foundation 0205