Sign in Join
Wiki copytemporary

macOS NSString Regular Expressions: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1+[![macOSlogo.png](/wiki-images/1/15/macOSlogo.png)](</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+
19## Regular expressions
210
3-Regular expressions are patterns used to match specified alpha-numeric character combinations in the string data being searched.
11+Regular expressions are patterns used to match specified alpha-numeric character combinations in the string data being searched.
412
5-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.
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.
614
7-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/).
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/>).
816
917## NSString Regular Expression Overview
1018
11-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.
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.
1220
1321### Metacharacters
1422
15-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).
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>).
1624
1725### Operators
1826
19-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).
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>).
2028
2129## Example 1 - match a pattern
2230
2331### Code
24-
25-``` pascal
32+```
2633Program regex_ex1;
27-
28-{$mode objfpc}{$H+}
29-{$modeswitch objectivec2}
30-
31-Uses
32- MacOSAll, CocoaAll, SysUtils;
33-
34-Var
35- srchStr : String;
36- patnStr : String;
37- i: NSRange;
38- j: NSRange;
39-
40-Begin
41- srchStr := 'I have 43 bags of 60 marbles.';
42- patnStr := '\d+';
43-
44- // Find first match
45- i := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(0, srchStr.Length));
46-
47- // Find second match
48- j := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length)));
49-
50- // Output
51- NSLog(NSStr('Search string: %@'), NSStr(srchStr));
52- NSLog(NSStr('Pattern string: %@'), NSStr(patnStr));
53-
54- NSLog(NSStr('Range 1: %@'), NSStringFromRange(i));
55- NSLog(NSStr('Location: %d'), i.location);
56- NSLog(NSStr('Length: %d'), i.length);
57- NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(i));
58-
59- NSLog(NSStr('Range 2: %@'), NSStringFromRange(j));
60- NSLog(NSStr('Location: %d'), j.location);
61- NSLog(NSStr('Length: %d'), j.length);
62- NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(j));
63-End.
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.
6471```
6572
6673### Output
67-
68-```text
74+```
69752021-06-15 21:43:49.049 regex_ex1[23182:177970] Search string: I have 43 bags of 60 marbles.
70-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Pattern string: \d+
71-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Range 1: {7, 2}
72-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Location: 7
73-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Length: 2
74-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Match: 43
75-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Range 2: {18, 2}
76-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Location: 18
77-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Length: 2
78-2021-06-15 21:43:49.049 regex_ex1[23182:177970] Match: 60
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
7985```
8086
8187## Example 2 - replace matched pattern
8288
8389### Code
84-
85-``` pascal
90+```
8691Program regex_ex2;
87-
88-{$mode objfpc}{$H+}
89-{$modeswitch objectivec2}
90-
91-Uses
92- MacOSAll, CocoaAll, SysUtils;
93-
94-Var
95- srchStr : NSString;
96- patnStr : NSString;
97- resStr : NSString;
98- i: NSRange;
99- j: NSRange;
100-
101-Begin
102- srchStr := NSStr('I have 43 bags of 60 marbles.');
103- patnStr := NSStr('\d+');
104-
105- // Find first match
106- i := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(0, srchStr.Length));
107-
108- // Do first replacement
109- resStr := srchStr.stringByReplacingOccurrencesOfString_withString(NSStr('43'), NSStr('forty-three'));
110-
111- // Find second match
112- j := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length)));
113-
114- // Do second replacement
115- resStr := resStr.stringByReplacingOccurrencesOfString_withString(NSStr('60'), NSStr('sixty'));
116-
117- // Output
118- NSLog(NSStr('Search string: %@'), srchStr);
119- NSLog(NSStr('Pattern string: %@'), patnStr);
120-
121- NSLog(NSStr('Range 1: %@'), NSStringFromRange(i));
122- NSLog(NSStr('Location: %d'), i.location);
123- NSLog(NSStr('Length: %d'), i.length);
124- NSLog(NSStr('Match: %@'), srchStr.substringWithRange(i));
125-
126- NSLog(NSStr('Range 2: %@'), NSStringFromRange(j));
127- NSLog(NSStr('Location: %d'), j.location);
128- NSLog(NSStr('Length: %d'), j.length);
129- NSLog(NSStr('Match: %@'), srchStr.substringWithRange(j));
130-
131- NSLog(NSStr('Result: %@'), resStr);
132-End.
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.
133138```
134139
135140### Output
136-
137-```text
141+```
1381422021-06-16 11:41:24.025 regex_ex2[5761:21687] Search string: I have 43 bags of 60 marbles.
139-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Pattern string: \d+
140-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Range 1: {7, 2}
141-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Location: 7
142-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Length: 2
143-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Match: 43
144-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Range 2: {18, 2}
145-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Location: 18
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: 60
148-2021-06-16 11:41:24.026 regex_ex2[5761:21687] Result: I have forty-three bags of sixty marbles.
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.
149153```
150154
151155## See also
152156
153-- [[macOS NSRegularExpression]].
154-- [[RegEx packages]] - cross platform.
157+ * [[macOS NSRegularExpression]].
158+ * [[RegEx packages]] \- cross platform.
155159
156160## External links
157161
158-- [Apple: NSString](https://developer.apple.com/documentation/foundation/nsstring).
159-- [Apple: rangeOfString:options:range:](https://developer.apple.com/documentation/foundation/nsstring/1415073-rangeofstring).
160-- [Apple: NSStringCompareOptions](https://developer.apple.com/documentation/foundation/nsstringcompareoptions).
161-- [Apple: NSRegularExpressionSearch](https://developer.apple.com/documentation/foundation/nsstringcompareoptions/nsregularexpressionsearch).
162-- [Apple: stringByReplacingOccurrencesOfString:withString:](https://developer.apple.com/documentation/foundation/nsstring/1412937-stringbyreplacingoccurrencesofst).
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>).
163167
164168---
165-*From the Free Pascal wiki, page "macOS NSString Regular Expressions", as it stood on 2021-08-07 after a change by Trev, out of the wiki's dump of 2024-05-26. 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).*
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).*

Back to history