macOS NSRegularExpression: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [](</File:macOSlogo.png>) | |
| 2 | + | ||
| 3 | + | This article applies to [macOS](</Category:macOS> "Category:macOS") only. | |
| 4 | + | ||
| 5 | + | See also: [[Multiplatform Programming Guide]] | |
| 6 | + | ||
| 7 | + | ||
| 8 | + | ||
| 1 | 9 | ## Regular expressions | |
| 2 | 10 | ||
| 3 | - | ||
| 11 | + | Regular expressions are patterns used to match specified alpha-numeric character combinations in the string data being searched. | |
| 4 | 12 | ||
| 5 | - | ||
| 13 | + | Each character in a regular expression (that is, each character in the string describing its pattern) is either a metacharacter or operator, having a special meaning, or a regular character that has a literal meaning. | |
| 6 | 14 | ||
| 7 | - | ||
| 15 | + | Regular expressions can be incredibly complex. Indeed, whole books have been written about them! For a gentle introduction to regular expressions, see this [O'Reilly article](<https://www.oreilly.com/content/an-introduction-to-regular-expressions/>). | |
| 8 | 16 | ||
| 9 | 17 | ## NSRegularExpression Overview | |
| 10 | 18 | ||
| 11 | - | ||
| 19 | + | The NSRegularExpression class has convenience methods for returning all the matches as an array, the total number of matches, the first match, and the range of the first match. | |
| 12 | 20 | ||
| 13 | - | ||
| 21 | + | An individual match is represented by an instance of the NSTextCheckingResult class, which carries information about the overall matched range (via its range property), and the range of each individual capture group (via the rangeAtIndex method). | |
| 14 | 22 | ||
| 15 | - | ||
| 23 | + | NSRegularExpression conforms to the International Components for Unicode (ICU) specification for regular expressions. | |
| 16 | 24 | ||
| 17 | 25 | ### Metacharacters | |
| 18 | 26 | ||
| 19 | - | ||
| 27 | + | For a comprehensive list of characters used by the NSRegularExpression class that have a special meaning in regular expression patterns, see the [ICU listing](<https://unicode-org.github.io/icu/userguide/strings/regexp.html#regular-expression-metacharacters>). | |
| 20 | 28 | ||
| 21 | 29 | ### Operators | |
| 22 | 30 | ||
| 23 | - | ||
| 31 | + | For a comprehensive list of operators used by the NSRegularExpression class, see the [ICU listing](<https://unicode-org.github.io/icu/userguide/strings/regexp.html#regular-expression-operators>). | |
| 24 | 32 | ||
| 25 | 33 | ## Example 1 - match a pattern | |
| 26 | 34 | ||
| 27 | - | ||
| 35 | + | In this fairly trivial and contrived code example, we use the **\d** metacharacter which matches a decimal digit and the **+** operator to match the preceding decimal digit one or more times. This pattern **\d+** aims to match all the occurrences of numbers in the search string which we then output using NSLog(). It uses the NSRegularExpression convenience methods for returning all of the matches in the search string as an array and the total number of matches. | |
| 28 | 36 | ||
| 29 | 37 | ### Code | |
| 30 | - | ||
| 31 | - | ||
| 38 | + | ``` | |
| 32 | 39 | Program regex_ex1; | |
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 40 | + | ||
| 41 | + | {$mode objfpc}{$H+} | |
| 42 | + | {$modeswitch objectivec2} | |
| 43 | + | ||
| 44 | + | Uses | |
| 45 | + | MacOSAll, CocoaAll, SysUtils; | |
| 46 | + | ||
| 47 | + | Var | |
| 48 | + | srchStr : String; | |
| 49 | + | patnStr : String; | |
| 50 | + | myRegex : NSregularExpression; | |
| 51 | + | matches : NSArray; | |
| 52 | + | match : NSTextCheckingResult; | |
| 53 | + | error : NSErrorPtr; | |
| 54 | + | ||
| 55 | + | Begin | |
| 56 | + | error := Nil; | |
| 57 | + | srchStr := 'I have 43 bags of 60 marbles.'; | |
| 58 | + | patnStr := '\d+'; | |
| 59 | + | ||
| 60 | + | // Create a regular expression with given string and options | |
| 61 | + | myRegex := NSregularExpression.regularExpressionWithPattern_options_error(NSStr(patnStr), NSRegularExpressionOptions(NSRegularExpressionCaseInsensitive), error); | |
| 62 | + | ||
| 63 | + | // Check creation of regular expression with given string and options | |
| 64 | + | if(error <> Nil) then | |
| 65 | + | begin | |
| 66 | + | NSLog(NSStr('Regex creation error: %@'), error); | |
| 67 | + | Exit; | |
| 68 | + | end; | |
| 69 | + | ||
| 70 | + | // Save any matches in the given string in the matches array | |
| 71 | + | matches := myRegex.matchesInString_options_range(NSStr(srchStr), 0, NSMakeRange(0, srchStr.Length)); | |
| 72 | + | ||
| 73 | + | // Output | |
| 74 | + | NSLog(NSStr('Search string: %@'), NSStr(srchStr)); | |
| 75 | + | NSLog(NSStr('Pattern string: %@'), NSStr(patnStr)); | |
| 76 | + | NSLog(NSStr('Number of matches: %lu'), myRegex.numberOfMatchesInString_options_range(NSStr(srchStr), 0, NSMakeRange(0, srchStr.Length))); | |
| 77 | + | ||
| 78 | + | for match in matches do | |
| 79 | + | NSLog(NSStr('match: %@'), NSStr(srchStr).substringWithRange(match.rangeAtIndex(0))); | |
| 80 | + | End. | |
| 74 | 81 | ``` | |
| 75 | 82 | ||
| 76 | 83 | ### Output | |
| 77 | 84 | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 85 | + | The output from running the above code example is: | |
| 86 | + | ``` | |
| 81 | 87 | 2021-06-12 21:05:46.335 regex_ex1[26138:232243] Search string: I have 43 bags of 60 marbles. | |
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 88 | + | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] Pattern string: \d+ | |
| 89 | + | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] Number of matches: 2 | |
| 90 | + | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] match: 43 | |
| 91 | + | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] match: 60 | |
| 86 | 92 | ``` | |
| 87 | 93 | ||
| 88 | 94 | ### Code explanation | |
| 89 | 95 | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 96 | + | The call to _regularExpressionWithPattern_options_error()_ creates an NSRegularExpression object instance (myRegex) with the specified regular expression pattern and options. | |
| 93 | 97 | ||
| 94 | - | ||
| 98 | + | Options are specified using _NSRegularExpressionOptions()_. Note that by default NSRegularExpression performs case-sensitive searches, so we specified the NSRegularExpressionCaseInsensitive option for case-insenstive searches although, because we are dealing with digits above, this has no effect and we might as well have specified Nil in this example for no options. | |
| 95 | 99 | ||
| 96 | - | ||
| 100 | + | Once we have the NSRegularExpression object, we can then use it for matching text among other operations. | |
| 97 | 101 | ||
| 98 | - | ||
| 102 | + | After checking that the creation of the regular expression did not fail with an error, we call the _matchesInString_options_range()_ method to search for any matches and store them in our NSArray (matches). This method takes our search string, any options (there are none here) and the range to search. The range is specified by giving _NSMakeRange()_ the starting location to search in the string (0 = the beginning of the string) and the length of the search string. | |
| 99 | 103 | ||
| 100 | - | ||
| 104 | + | Next, we output the search string and pattern string, and then call the _numberOfMatchesInString_options_range()_ method to determine the number of matches and output it. | |
| 101 | 105 | ||
| 102 | - | ||
| 106 | + | Finally, we iterate through the matches NSArray and output the matches individually. The call to _rangeAtIndex(0)_ is the full match and is equivalent to simply calling _range_. The code for this looks a little obscure. Let me try to unpack it for you. | |
| 103 | 107 | ||
| 104 | - | ||
| 108 | + | If you just output the content of the matches NSArray you get this: | |
| 109 | + | ``` | |
| 105 | 110 | "<NSSimpleRegularExpressionCheckingResult: 0x14d617570>{7, 2}{<NSRegularExpression: 0x14d614fd0> \\d+ 0x1}", | |
| 106 | - | ||
| 111 | + | "<NSSimpleRegularExpressionCheckingResult: 0x14d617610>{18, 2}{<NSRegularExpression: 0x14d614fd0> \\d+ 0x1}" | |
| 107 | 112 | ``` | |
| 108 | 113 | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 114 | + | Notice the {7, 2} and {18, 2} ranges which locate the first number at position 7 (counting from zero) in the search string with a length of 2 and the second number at position 18 with a length of 2. Knowing those ranges, you could use: | |
| 115 | + | ``` | |
| 112 | 116 | NSLog(NSStr('match: ''%@'''), NSStr(srchStr).substringWithRange(NSMakeRange(7, 2))); | |
| 113 | 117 | ``` | |
| 114 | 118 | ||
| 115 | - | ||
| 119 | + | to output the first number. The _substringWithRange()_ method extracts from our search string the substring that matches the specified range (7, 2). Clearer than mud? I hope so. | |
| 116 | 120 | ||
| 117 | 121 | ## Example 2 - match pattern groups | |
| 118 | 122 | ||
| 119 | - | ||
| 123 | + | This is similar to Example 1 above, except that this time we match groups of characters. Our search string is the same as before, but our pattern string has some added complexity. The pattern matches a decimal digit one or more times as before, but this time as a group which is delineated by using parentheses: **(/d+)**. Next, we use a point **.** to match any character and **?** to match zero or one times following the digit(s). Finally, we match the set of characters from a to z **([a-z]+)** one or more times as a group. | |
| 120 | 124 | ||
| 121 | 125 | ### Code | |
| 122 | - | ||
| 123 | - | ||
| 126 | + | ``` | |
| 124 | 127 | Program regex_ex2; | |
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 132 | - | ||
| 133 | - | ||
| 134 | - | ||
| 135 | - | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | - | ||
| 168 | - | ||
| 169 | - | ||
| 128 | + | ||
| 129 | + | {$mode objfpc}{$H+} | |
| 130 | + | {$modeswitch objectivec2} | |
| 131 | + | ||
| 132 | + | Uses | |
| 133 | + | MacOSAll, CocoaAll, SysUtils; | |
| 134 | + | ||
| 135 | + | Var | |
| 136 | + | srchStr : String; | |
| 137 | + | patnStr : String; | |
| 138 | + | myRegex : NSregularExpression; | |
| 139 | + | matches : NSArray; | |
| 140 | + | match : NSTextCheckingResult; | |
| 141 | + | error : NSErrorPtr; | |
| 142 | + | ||
| 143 | + | Begin | |
| 144 | + | error := Nil; | |
| 145 | + | srchStr := 'I have 43 Bags of 60 Marbles.'; | |
| 146 | + | patnStr := '(\d+).?([a-z]+)'; | |
| 147 | + | ||
| 148 | + | // Create a regular expression with given string and options | |
| 149 | + | myRegex := NSregularExpression.regularExpressionWithPattern_options_error(NSStr(patnStr), NSRegularExpressionOptions(NSRegularExpressionCaseInsensitive), error); | |
| 150 | + | ||
| 151 | + | // Check creation of regular expression with given string and options | |
| 152 | + | if(error <> Nil) then | |
| 153 | + | begin | |
| 154 | + | NSLog(NSStr('Regex creation error: %@'), error); | |
| 155 | + | Exit; | |
| 156 | + | end; | |
| 157 | + | ||
| 158 | + | // Save any matches in the given string in the matches array | |
| 159 | + | matches := myRegex.matchesInString_options_range(NSStr(srchStr), 0, NSMakeRange(0, srchStr.Length)); | |
| 160 | + | ||
| 161 | + | // Output | |
| 162 | + | NSLog(NSStr('Search string: %@'), NSStr(srchStr)); | |
| 163 | + | NSLog(NSStr('Pattern string: %@'), NSStr(patnStr)); | |
| 164 | + | NSLog(NSStr('Number of matches: %lu'), myRegex.numberOfMatchesInString_options_range(NSStr(srchStr), 0, NSMakeRange(0, srchStr.Length))); | |
| 165 | + | ||
| 166 | + | for match in matches do | |
| 167 | + | begin | |
| 168 | + | NSLog(NSStr('match(0): ''%@'''), NSStr(srchStr).substringWithRange(match.rangeAtIndex(0))); | |
| 169 | + | NSLog(NSStr('match(1): ''%@'''), NSStr(srchStr).substringWithRange(match.rangeAtIndex(1))); | |
| 170 | + | NSLog(NSStr('match(2): ''%@'''), NSStr(srchStr).substringWithRange(match.rangeAtIndex(2))); | |
| 171 | + | end; | |
| 172 | + | End. | |
| 170 | 173 | ``` | |
| 171 | 174 | ||
| 172 | 175 | ### Output | |
| 173 | - | ||
| 174 | - | ||
| 176 | + | ``` | |
| 175 | 177 | 2021-06-14 17:39:51.255 program2[7376:149163] Search string: I have 43 bags of 60 marbles. | |
| 176 | - | ||
| 177 | - | ||
| 178 | - | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 178 | + | 2021-06-14 17:39:51.255 program2[7376:149163] Pattern string: (\d+).?([a-z]+) | |
| 179 | + | 2021-06-14 17:39:51.255 program2[7376:149163] Number of matches: 2 | |
| 180 | + | 2021-06-14 17:39:51.255 program2[7376:149163] match(0): '43 Bags' | |
| 181 | + | 2021-06-14 17:39:51.255 program2[7376:149163] match(1): '43' | |
| 182 | + | 2021-06-14 17:39:51.255 program2[7376:149163] match(2): 'Bags' | |
| 183 | + | 2021-06-14 17:39:51.255 program2[7376:149163] match(0): '60 Marbles' | |
| 184 | + | 2021-06-14 17:39:51.255 program2[7376:149163] match(1): '60' | |
| 185 | + | 2021-06-14 17:39:51.255 program2[7376:149163] match(2): 'Marbles' | |
| 184 | 186 | ``` | |
| 185 | 187 | ||
| 186 | 188 | ### Code explanation | |
| 187 | 189 | ||
| 188 | - | ||
| 190 | + | The explanation is pretty much the same as for Example 1, except that: | |
| 189 | 191 | ||
| 190 | - | ||
| 192 | + | 1) The NSRegularExpressionCaseInsensitive option for case-insenstive searches now has a use. We specified the set of lowercase characters a to z, but because of the option we matched the words Bags and Marbles with initial capital letters. | |
| 191 | 193 | ||
| 192 | - | ||
| 194 | + | 2) The call to _rangeAtIndex(0)_ or the equivalent _range_ matches the full pattern; the call to _rangeAtIndex(1)_ matches the first group of digits that we specified; and the call to _rangeAtIndex(2)_ matches the second group of characters that we specified. | |
| 193 | 195 | ||
| 194 | 196 | ## Example 3 - replace matched pattern | |
| 195 | 197 | ||
| 196 | - | ||
| 198 | + | This is similar to Example 1 above, except that this time we replace the decimal numbers matched by the regular expression with words instead of numbers. This is done two ways: one uses the _stringByReplacingMatchesInString_options_range_withTemplate()_ method which returns a new string with the replacements and the other uses the _replaceMatchesInString_options_range_withTemplate()_ method which replaces the matches in the original search string. | |
| 197 | 199 | ||
| 198 | 200 | ### Code | |
| 199 | - | ||
| 200 | - | ||
| 201 | + | ``` | |
| 201 | 202 | Program regex_ex3; | |
| 202 | - | ||
| 203 | - | ||
| 204 | - | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - | ||
| 208 | - | ||
| 209 | - | ||
| 210 | - | ||
| 211 | - | ||
| 212 | - | ||
| 213 | - | ||
| 214 | - | ||
| 215 | - | ||
| 216 | - | ||
| 217 | - | ||
| 218 | - | ||
| 219 | - | ||
| 220 | - | ||
| 221 | - | ||
| 222 | - | ||
| 223 | - | ||
| 224 | - | ||
| 225 | - | ||
| 226 | - | ||
| 227 | - | ||
| 228 | - | ||
| 229 | - | ||
| 230 | - | ||
| 231 | - | ||
| 232 | - | ||
| 233 | - | ||
| 234 | - | ||
| 235 | - | ||
| 236 | - | ||
| 237 | - | ||
| 238 | - | ||
| 239 | - | ||
| 240 | - | ||
| 241 | - | ||
| 242 | - | ||
| 243 | - | ||
| 244 | - | ||
| 245 | - | ||
| 246 | - | ||
| 247 | - | ||
| 248 | - | ||
| 249 | - | ||
| 250 | - | ||
| 251 | - | ||
| 203 | + | ||
| 204 | + | {$mode objfpc}{$H+} | |
| 205 | + | {$modeswitch objectivec2} | |
| 206 | + | ||
| 207 | + | Uses | |
| 208 | + | MacOSAll, CocoaAll, SysUtils; | |
| 209 | + | ||
| 210 | + | Var | |
| 211 | + | srchStr : NSString; | |
| 212 | + | srchStr2: NSMutableString; | |
| 213 | + | patnStr : NSString; | |
| 214 | + | templStr: NSString; | |
| 215 | + | myRegex : NSregularExpression; | |
| 216 | + | matches : NSArray; | |
| 217 | + | match : NSTextCheckingResult; | |
| 218 | + | numMatch: NSInteger; | |
| 219 | + | error : NSErrorPtr; | |
| 220 | + | count : ShortInt; | |
| 221 | + | ||
| 222 | + | Begin | |
| 223 | + | error := Nil; | |
| 224 | + | srchStr := NSStr('I have 43 Bags of 60 Marbles.'); | |
| 225 | + | srchStr2:= NSMutableString.stringWithString(srchStr); | |
| 226 | + | patnStr := NSStr('\d+'); | |
| 227 | + | ||
| 228 | + | // Create a regular expression with given string and options | |
| 229 | + | myRegex := NSregularExpression.regularExpressionWithPattern_options_error(patnStr, NSRegularExpressionOptions(NSRegularExpressionCaseInsensitive), error); | |
| 230 | + | ||
| 231 | + | // Check creation of regular expression with given string and options | |
| 232 | + | if(error <> Nil) then | |
| 252 | 233 | begin | |
| 253 | - | ||
| 254 | - | ||
| 255 | - | ||
| 256 | - | ||
| 257 | - | ||
| 258 | - | ||
| 259 | - | ||
| 260 | - | ||
| 261 | - | ||
| 262 | - | ||
| 263 | - | ||
| 264 | - | ||
| 265 | - | ||
| 266 | - | ||
| 234 | + | NSLog(NSStr('Regex creation error: %@'), error); | |
| 235 | + | Exit; | |
| 267 | 236 | end; | |
| 268 | - | ||
| 269 | - | ||
| 270 | - | ||
| 271 | - | ||
| 272 | - | ||
| 273 | - | ||
| 274 | - | ||
| 275 | - | ||
| 276 | - | ||
| 277 | - | ||
| 278 | - | ||
| 279 | - | ||
| 280 | - | ||
| 281 | - | ||
| 282 | - | ||
| 237 | + | ||
| 238 | + | // Save any matches in the given string in the matches array | |
| 239 | + | matches := myRegex.matchesInString_options_range(srchStr, 0, NSMakeRange(0, srchStr.Length)); | |
| 240 | + | ||
| 241 | + | // Save number of matches | |
| 242 | + | numMatch := myRegex.numberOfMatchesInString_options_range(srchStr, 0, NSMakeRange(0, srchStr.Length)); | |
| 243 | + | ||
| 244 | + | // Output | |
| 245 | + | NSLog(NSStr('Search string: %@'), srchStr); | |
| 246 | + | NSLog(NSStr('Pattern string: %@'), patnStr); | |
| 247 | + | NSLog(NSStr('Number of matches: %lu'), numMatch); | |
| 248 | + | ||
| 249 | + | // Alternative 1: Using stringByReplacingMatchesInString_options_range_withTemplate() | |
| 250 | + | for count := numMatch downto 1 do | |
| 283 | 251 | begin | |
| 284 | - | ||
| 252 | + | for match in matches do | |
| 285 | 253 | begin | |
| 286 | - | ||
| 287 | - | ||
| 254 | + | if(srchStr.substringWithRange(match.range) = NSStr('43')) then | |
| 255 | + | begin | |
| 256 | + | templStr := NSStr('forty-three'); | |
| 257 | + | NSLog(NSStr('Match 1: %@ - Template string: %@'), srchStr.substringWithRange(match.range), templStr); | |
| 258 | + | end; | |
| 259 | + | ||
| 260 | + | if(srchStr.substringWithRange(match.range) = NSStr('60')) then | |
| 261 | + | begin | |
| 262 | + | templStr := NSStr('sixty'); | |
| 263 | + | NSLog(NSStr('Match 2: %@ - Template string: %@'), srchStr.substringWithRange(match.range), templStr); | |
| 264 | + | end; | |
| 265 | + | ||
| 266 | + | // Do string replacement | |
| 267 | + | srchStr := myRegex.stringByReplacingMatchesInString_options_range_withTemplate(srchStr, NSRegularExpressionOptions(Nil), match.range, templStr); | |
| 288 | 268 | end; | |
| 289 | - | ||
| 269 | + | ||
| 270 | + | // Save any matches in the new search string in the matches array | |
| 271 | + | matches := myRegex.matchesInString_options_range(srchStr, 0, NSMakeRange(0, srchStr.Length)); | |
| 272 | + | end; | |
| 273 | + | ||
| 274 | + | // Output string with replacements | |
| 275 | + | NSLog(NSStr('Result: %@'), srchStr); | |
| 276 | + | ||
| 277 | + | // Save any matches in the given string in the matches array | |
| 278 | + | matches := myRegex.matchesInString_options_range(srchStr2, 0, NSMakeRange(0, srchStr2.Length)); | |
| 279 | + | ||
| 280 | + | // Alternative 2: Using replaceMatchesInString_options_range_withTemplate() | |
| 281 | + | for count := numMatch downto 1 do | |
| 282 | + | begin | |
| 283 | + | for match in matches do | |
| 290 | 284 | begin | |
| 291 | - | ||
| 292 | - | ||
| 293 | - | ||
| 294 | - | ||
| 295 | - | ||
| 296 | - | ||
| 297 | - | ||
| 298 | - | ||
| 299 | - | ||
| 300 | - | ||
| 301 | - | ||
| 302 | - | ||
| 303 | - | ||
| 304 | - | ||
| 305 | - | ||
| 285 | + | if(srchStr2.substringWithRange(match.range) = NSStr('43')) then | |
| 286 | + | begin | |
| 287 | + | templStr := NSStr('forty-three'); | |
| 288 | + | NSLog(NSStr('Match 1: %@ - Template string: %@'), srchStr2.substringWithRange(match.range), templStr); | |
| 289 | + | end; | |
| 290 | + | if(srchStr2.substringWithRange(match.range) = NSStr('60')) then | |
| 291 | + | begin | |
| 292 | + | templStr := NSStr('sixty'); | |
| 293 | + | NSLog(NSStr('Match 2: %@ - Template string: %@'), srchStr2.substringWithRange(match.range), templStr); | |
| 294 | + | end; | |
| 295 | + | ||
| 296 | + | // Do string replacement | |
| 297 | + | myRegex.replaceMatchesInString_options_range_withTemplate(srchStr2, NSRegularExpressionOptions(Nil), match.range, templStr); | |
| 298 | + | end; | |
| 299 | + | ||
| 300 | + | // Save any matches in the new search string in the matches array | |
| 301 | + | matches := myRegex.matchesInString_options_range(srchStr2, 0, NSMakeRange(0, srchStr2.Length)); | |
| 302 | + | end; | |
| 303 | + | ||
| 304 | + | // Output string with replacements | |
| 305 | + | NSLog(NSStr('Result: %@'), srchStr2); | |
| 306 | + | End. | |
| 306 | 307 | ``` | |
| 307 | 308 | ||
| 308 | 309 | ### Output | |
| 309 | - | ||
| 310 | - | ||
| 310 | + | ``` | |
| 311 | 311 | 2021-06-14 22:42:55.807 program3[9115:227765] Search string: I have 43 Bags of 60 Marbles. | |
| 312 | - | ||
| 313 | - | ||
| 314 | - | ||
| 315 | - | ||
| 316 | - | ||
| 317 | - | ||
| 318 | - | ||
| 319 | - | ||
| 312 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Pattern string: \d+ | |
| 313 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Number of matches: 2 | |
| 314 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Match 1: 43 - Template string: forty-three | |
| 315 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Match 2: 60 - Template string: sixty | |
| 316 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Result: I have forty-three Bags of sixty Marbles. | |
| 317 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Match 1: 43 - Template string: forty-three | |
| 318 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Match 2: 60 - Template string: sixty | |
| 319 | + | 2021-06-14 22:42:55.808 program3[9115:227765] Result: I have forty-three Bags of sixty Marbles. | |
| 320 | 320 | ``` | |
| 321 | 321 | ||
| 322 | 322 | ## Example 4 - rearrange matched pattern groups | |
| 323 | 323 | ||
| 324 | - | ||
| 324 | + | This is similar to Example 2 above, except that this time not only do we match groups of characters, we rearrange them. Our search string is different this time and represents a contact list. We are going to rearrange the format from Firstname, Lastname to the more sensible Lastname, Firstname and then enclose the contact number state area code in parentheses. To do this, our pattern string has some added complexity. | |
| 325 | 325 | ||
| 326 | - | ||
| 326 | + | The pattern matches the first word group as a group of word characters followed by a comma **(\w+),** and does exactly the same again for the second word group, and finally matches the two digits of the area code as a group **(\d{2})**. The curly braces enclosing the number **{2}** after the digit metacharacter specifies that we want to match exactly two digits which comprise the area code. | |
| 327 | 327 | ||
| 328 | - | ||
| 328 | + | The template string rearranges the data groups by switching the order of the first two word groups and adding parentheses around the third group which comprises the area code number. | |
| 329 | 329 | ||
| 330 | 330 | ### Code | |
| 331 | - | ||
| 332 | - | ||
| 331 | + | ``` | |
| 333 | 332 | Program regex_ex4; | |
| 334 | - | ||
| 335 | - | ||
| 336 | - | ||
| 337 | - | ||
| 338 | - | ||
| 339 | - | ||
| 340 | - | ||
| 341 | - | ||
| 342 | - | ||
| 343 | - | ||
| 344 | - | ||
| 345 | - | ||
| 346 | - | ||
| 347 | - | ||
| 348 | - | ||
| 349 | - | ||
| 350 | - | ||
| 351 | - | ||
| 352 | - | ||
| 353 | - | ||
| 354 | - | ||
| 355 | - | ||
| 356 | - | ||
| 357 | - | ||
| 358 | - | ||
| 359 | - | ||
| 360 | - | ||
| 361 | - | ||
| 362 | - | ||
| 363 | - | ||
| 364 | - | ||
| 365 | - | ||
| 366 | - | ||
| 367 | - | ||
| 368 | - | ||
| 369 | - | ||
| 370 | - | ||
| 371 | - | ||
| 372 | - | ||
| 373 | - | ||
| 374 | - | ||
| 375 | - | ||
| 376 | - | ||
| 377 | - | ||
| 378 | - | ||
| 379 | - | ||
| 380 | - | ||
| 381 | - | ||
| 382 | - | ||
| 383 | - | ||
| 384 | - | ||
| 385 | - | ||
| 386 | - | ||
| 387 | - | ||
| 333 | + | ||
| 334 | + | {$mode objfpc}{$H+} | |
| 335 | + | {$modeswitch objectivec2} | |
| 336 | + | ||
| 337 | + | Uses | |
| 338 | + | MacOSAll, CocoaAll, SysUtils; | |
| 339 | + | ||
| 340 | + | Var | |
| 341 | + | srchStr : NSString; | |
| 342 | + | srchStr2: NSMutableString; | |
| 343 | + | patnStr : NSString; | |
| 344 | + | tmplStr : NSString; | |
| 345 | + | myRegex : NSregularExpression; | |
| 346 | + | matches : NSArray; | |
| 347 | + | match : NSTextCheckingResult; | |
| 348 | + | error : NSErrorPtr; | |
| 349 | + | ||
| 350 | + | Begin | |
| 351 | + | error := Nil; | |
| 352 | + | srchStr := NSStr('Firstname, Lastname, 02 9428 4687'); | |
| 353 | + | srchStr2:= NSMutableString.stringWithString(srchStr); | |
| 354 | + | patnStr := NSStr('(\w+), (\w+), (\d{2})'); | |
| 355 | + | tmplStr := NSStr('$2, $1, ($3)'); | |
| 356 | + | ||
| 357 | + | // Create a regular expression with given string and options | |
| 358 | + | myRegex := NSregularExpression.regularExpressionWithPattern_options_error(patnStr, NSRegularExpressionOptions(NSRegularExpressionCaseInsensitive), error); | |
| 359 | + | ||
| 360 | + | // Check creation of regular expression with given string and options | |
| 361 | + | if(error <> Nil) then | |
| 362 | + | begin | |
| 363 | + | NSLog(NSStr('Regex creation error: %@'), error); | |
| 364 | + | Exit; | |
| 365 | + | end; | |
| 366 | + | ||
| 367 | + | // Save any matches in the given string in the matches array | |
| 368 | + | matches := myRegex.matchesInString_options_range(srchStr, 0, NSMakeRange(0, srchStr.Length)); | |
| 369 | + | ||
| 370 | + | // Output | |
| 371 | + | NSLog(NSStr('Search string: %@'), srchStr); | |
| 372 | + | NSLog(NSStr('Pattern string: %@'), patnStr); | |
| 373 | + | NSLog(NSStr('Template string: %@'), tmplStr); | |
| 374 | + | NSLog(NSStr('Number of matches: %lu'), myRegex.numberOfMatchesInString_options_range(srchStr, 0, NSMakeRange(0, srchStr.Length))); | |
| 375 | + | ||
| 376 | + | for match in matches do | |
| 377 | + | begin | |
| 378 | + | NSLog(NSStr('match(1): %@'), srchStr.substringWithRange(match.rangeAtIndex(1))); | |
| 379 | + | NSLog(NSStr('match(2): %@'), srchStr.substringWithRange(match.rangeAtIndex(2))); | |
| 380 | + | NSLog(NSStr('match(3): %@'), srchStr.substringWithRange(match.rangeAtIndex(3))); | |
| 381 | + | // Do the replacements | |
| 382 | + | myRegex.replaceMatchesInString_options_range_withTemplate(srchStr2, NSRegularExpressionOptions(Nil), match.range, tmplStr); | |
| 383 | + | end; | |
| 384 | + | ||
| 385 | + | NSLog(NSStr('Result: %@'), srchStr2); | |
| 386 | + | End. | |
| 388 | 387 | ``` | |
| 389 | 388 | ||
| 390 | 389 | ### Output | |
| 391 | - | ||
| 392 | - | ||
| 390 | + | ``` | |
| 393 | 391 | 2021-06-15 20:37:54.880 program4[23076:159228] Search string: Firstname, Lastname, 02 9428 4687 | |
| 394 | - | ||
| 395 | - | ||
| 396 | - | ||
| 397 | - | ||
| 398 | - | ||
| 399 | - | ||
| 400 | - | ||
| 392 | + | 2021-06-15 20:37:54.880 program4[23076:159228] Pattern string: (\w+), (\w+), (\d{2}) | |
| 393 | + | 2021-06-15 20:37:54.880 program4[23090:159932] Template string: $2, $1, ($3) | |
| 394 | + | 2021-06-15 20:37:54.881 program4[23076:159228] Number of matches: 1 | |
| 395 | + | 2021-06-15 20:37:54.881 program4[23076:159228] match(1): Firstname | |
| 396 | + | 2021-06-15 20:37:54.881 program4[23076:159228] match(2): Lastname | |
| 397 | + | 2021-06-15 20:37:54.881 program4[23076:159228] match(3): 02 | |
| 398 | + | 2021-06-15 20:37:54.881 program4[23076:159228] Result: Lastname, Firstname, (02) 9428 4687 | |
| 401 | 399 | ``` | |
| 402 | 400 | ||
| 403 | 401 | ## See also | |
| 404 | 402 | ||
| 405 | - | ||
| 406 | - | ||
| 403 | + | * [[macOS NSString Regular Expressions]]. | |
| 404 | + | * [[RegEx packages]] \- cross platform. | |
| 407 | 405 | ||
| 408 | 406 | ## External links | |
| 409 | 407 | ||
| 410 | - | ||
| 408 | + | * [Apple: NSRegularExpression](<https://developer.apple.com/documentation/foundation/nsregularexpression>). | |
| 411 | 409 | ||
| 412 | - | ||
| 410 | + | * [ICU: Documentation](<https://unicode-org.github.io/icu/>). | |
| 413 | 411 | ||
| 414 | 412 | --- | |
| 415 | - | ||
| 413 | + | *From the Free Pascal wiki, page "macOS NSRegularExpression", as the Internet Archive kept it on 2025-03-13, last changed on 2021-08-08. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
