macOS NSString Regular Expressions: 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 | ## NSString Regular Expression Overview | |
| 10 | 18 | ||
| 11 | - | ||
| 19 | + | It is a little known fact that you can use regular expressions to match and replace patterns without having to resort to the more sophisticated, but complicated, [[macOS NSRegularExpression|NSRegularExpression]]: NSString has the _rangeOfString:options:range:_ method for matching patterns and the _stringByReplacingOccurrencesOfString:withString:_ method for replacing patterns. These are demonstrated in the examples below. | |
| 12 | 20 | ||
| 13 | 21 | ### Metacharacters | |
| 14 | 22 | ||
| 15 | - | ||
| 23 | + | For a comprehensive list of characters used 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>). | |
| 16 | 24 | ||
| 17 | 25 | ### Operators | |
| 18 | 26 | ||
| 19 | - | ||
| 27 | + | For a comprehensive list of regular expression operators, see the [ICU listing](<https://unicode-org.github.io/icu/userguide/strings/regexp.html#regular-expression-operators>). | |
| 20 | 28 | ||
| 21 | 29 | ## Example 1 - match a pattern | |
| 22 | 30 | ||
| 23 | 31 | ### Code | |
| 24 | - | ||
| 25 | - | ||
| 32 | + | ``` | |
| 26 | 33 | Program regex_ex1; | |
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 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 | - | ||
| 34 | + | ||
| 35 | + | {$mode objfpc}{$H+} | |
| 36 | + | {$modeswitch objectivec2} | |
| 37 | + | ||
| 38 | + | Uses | |
| 39 | + | MacOSAll, CocoaAll, SysUtils; | |
| 40 | + | ||
| 41 | + | Var | |
| 42 | + | srchStr : String; | |
| 43 | + | patnStr : String; | |
| 44 | + | i: NSRange; | |
| 45 | + | j: NSRange; | |
| 46 | + | ||
| 47 | + | Begin | |
| 48 | + | srchStr := 'I have 43 bags of 60 marbles.'; | |
| 49 | + | patnStr := '\d+'; | |
| 50 | + | ||
| 51 | + | // Find first match | |
| 52 | + | i := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(0, srchStr.Length)); | |
| 53 | + | ||
| 54 | + | // Find second match | |
| 55 | + | j := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length))); | |
| 56 | + | ||
| 57 | + | // Output | |
| 58 | + | NSLog(NSStr('Search string: %@'), NSStr(srchStr)); | |
| 59 | + | NSLog(NSStr('Pattern string: %@'), NSStr(patnStr)); | |
| 60 | + | ||
| 61 | + | NSLog(NSStr('Range 1: %@'), NSStringFromRange(i)); | |
| 62 | + | NSLog(NSStr('Location: %d'), i.location); | |
| 63 | + | NSLog(NSStr('Length: %d'), i.length); | |
| 64 | + | NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(i)); | |
| 65 | + | ||
| 66 | + | NSLog(NSStr('Range 2: %@'), NSStringFromRange(j)); | |
| 67 | + | NSLog(NSStr('Location: %d'), j.location); | |
| 68 | + | NSLog(NSStr('Length: %d'), j.length); | |
| 69 | + | NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(j)); | |
| 70 | + | End. | |
| 64 | 71 | ``` | |
| 65 | 72 | ||
| 66 | 73 | ### Output | |
| 67 | - | ||
| 68 | - | ||
| 74 | + | ``` | |
| 69 | 75 | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Search string: I have 43 bags of 60 marbles. | |
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 76 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Pattern string: \d+ | |
| 77 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Range 1: {7, 2} | |
| 78 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Location: 7 | |
| 79 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Length: 2 | |
| 80 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Match: 43 | |
| 81 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Range 2: {18, 2} | |
| 82 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Location: 18 | |
| 83 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Length: 2 | |
| 84 | + | 2021-06-15 21:43:49.049 regex_ex1[23182:177970] Match: 60 | |
| 79 | 85 | ``` | |
| 80 | 86 | ||
| 81 | 87 | ## Example 2 - replace matched pattern | |
| 82 | 88 | ||
| 83 | 89 | ### Code | |
| 84 | - | ||
| 85 | - | ||
| 90 | + | ``` | |
| 86 | 91 | Program regex_ex2; | |
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 132 | - | ||
| 92 | + | ||
| 93 | + | {$mode objfpc}{$H+} | |
| 94 | + | {$modeswitch objectivec2} | |
| 95 | + | ||
| 96 | + | Uses | |
| 97 | + | MacOSAll, CocoaAll, SysUtils; | |
| 98 | + | ||
| 99 | + | Var | |
| 100 | + | srchStr : NSString; | |
| 101 | + | patnStr : NSString; | |
| 102 | + | resStr : NSString; | |
| 103 | + | i: NSRange; | |
| 104 | + | j: NSRange; | |
| 105 | + | ||
| 106 | + | Begin | |
| 107 | + | srchStr := NSStr('I have 43 bags of 60 marbles.'); | |
| 108 | + | patnStr := NSStr('\d+'); | |
| 109 | + | ||
| 110 | + | // Find first match | |
| 111 | + | i := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(0, srchStr.Length)); | |
| 112 | + | ||
| 113 | + | // Do first replacement | |
| 114 | + | resStr := srchStr.stringByReplacingOccurrencesOfString_withString(NSStr('43'), NSStr('forty-three')); | |
| 115 | + | ||
| 116 | + | // Find second match | |
| 117 | + | j := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length))); | |
| 118 | + | ||
| 119 | + | // Do second replacement | |
| 120 | + | resStr := resStr.stringByReplacingOccurrencesOfString_withString(NSStr('60'), NSStr('sixty')); | |
| 121 | + | ||
| 122 | + | // Output | |
| 123 | + | NSLog(NSStr('Search string: %@'), srchStr); | |
| 124 | + | NSLog(NSStr('Pattern string: %@'), patnStr); | |
| 125 | + | ||
| 126 | + | NSLog(NSStr('Range 1: %@'), NSStringFromRange(i)); | |
| 127 | + | NSLog(NSStr('Location: %d'), i.location); | |
| 128 | + | NSLog(NSStr('Length: %d'), i.length); | |
| 129 | + | NSLog(NSStr('Match: %@'), srchStr.substringWithRange(i)); | |
| 130 | + | ||
| 131 | + | NSLog(NSStr('Range 2: %@'), NSStringFromRange(j)); | |
| 132 | + | NSLog(NSStr('Location: %d'), j.location); | |
| 133 | + | NSLog(NSStr('Length: %d'), j.length); | |
| 134 | + | NSLog(NSStr('Match: %@'), srchStr.substringWithRange(j)); | |
| 135 | + | ||
| 136 | + | NSLog(NSStr('Result: %@'), resStr); | |
| 137 | + | End. | |
| 133 | 138 | ``` | |
| 134 | 139 | ||
| 135 | 140 | ### Output | |
| 136 | - | ||
| 137 | - | ||
| 141 | + | ``` | |
| 138 | 142 | 2021-06-16 11:41:24.025 regex_ex2[5761:21687] Search string: I have 43 bags of 60 marbles. | |
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 143 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Pattern string: \d+ | |
| 144 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Range 1: {7, 2} | |
| 145 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Location: 7 | |
| 146 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Length: 2 | |
| 147 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Match: 43 | |
| 148 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Range 2: {18, 2} | |
| 149 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Location: 18 | |
| 150 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Length: 2 | |
| 151 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Match: 60 | |
| 152 | + | 2021-06-16 11:41:24.026 regex_ex2[5761:21687] Result: I have forty-three bags of sixty marbles. | |
| 149 | 153 | ``` | |
| 150 | 154 | ||
| 151 | 155 | ## See also | |
| 152 | 156 | ||
| 153 | - | ||
| 154 | - | ||
| 157 | + | * [[macOS NSRegularExpression]]. | |
| 158 | + | * [[RegEx packages]] \- cross platform. | |
| 155 | 159 | ||
| 156 | 160 | ## External links | |
| 157 | 161 | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 162 | + | * [Apple: NSString](<https://developer.apple.com/documentation/foundation/nsstring>). | |
| 163 | + | * [Apple: rangeOfString:options:range:](<https://developer.apple.com/documentation/foundation/nsstring/1415073-rangeofstring>). | |
| 164 | + | * [Apple: NSStringCompareOptions](<https://developer.apple.com/documentation/foundation/nsstringcompareoptions>). | |
| 165 | + | * [Apple: NSRegularExpressionSearch](<https://developer.apple.com/documentation/foundation/nsstringcompareoptions/nsregularexpressionsearch>). | |
| 166 | + | * [Apple: stringByReplacingOccurrencesOfString:withString:](<https://developer.apple.com/documentation/foundation/nsstring/1412937-stringbyreplacingoccurrencesofst>). | |
| 163 | 167 | ||
| 164 | 168 | --- | |
| 165 | - | ||
| 169 | + | *From the Free Pascal wiki, page "macOS NSString Regular Expressions", as the Internet Archive kept it on 2024-10-04, 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).* |
