Sign in Join
Wiki copytemporary

macOS NSString Regular Expressions: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: macOS NSString Regular Expressions

OldNewLine
1+## Regular expressions
2+
3+Regular expressions are patterns used to match specified alpha-numeric character combinations in the string data being searched.
4+
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.
6+
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/).
8+
9+## NSString Regular Expression Overview
10+
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.
12+
13+### Metacharacters
14+
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).
16+
17+### Operators
18+
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).
20+
21+## Example 1 - match a pattern
22+
23+### Code
24+
25+``` pascal
26+Program 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.
64+```
65+
66+### Output
67+
68+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Search string: I have 43 bags of 60 marbles.`\
69+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Pattern string: \d+`\
70+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Range 1: {7, 2}`\
71+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Location: 7`\
72+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Length: 2`\
73+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Match: 43`\
74+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Range 2: {18, 2}`\
75+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Location: 18`\
76+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Length: 2`\
77+`2021-06-15 21:43:49.049 regex_ex1[23182:177970] Match: 60`
78+
79+## Example 2 - replace matched pattern
80+
81+### Code
82+
83+``` pascal
84+Program regex_ex2;
85+
86+{$mode objfpc}{$H+}
87+{$modeswitch objectivec2}
88+
89+Uses
90+ MacOSAll, CocoaAll, SysUtils;
91+
92+Var
93+ srchStr : NSString;
94+ patnStr : NSString;
95+ resStr : NSString;
96+ i: NSRange;
97+ j: NSRange;
98+
99+Begin
100+ srchStr := NSStr('I have 43 bags of 60 marbles.');
101+ patnStr := NSStr('\d+');
102+
103+ // Find first match
104+ i := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(0, srchStr.Length));
105+
106+ // Do first replacement
107+ resStr := srchStr.stringByReplacingOccurrencesOfString_withString(NSStr('43'), NSStr('forty-three'));
108+
109+ // Find second match
110+ j := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length)));
111+
112+ // Do second replacement
113+ resStr := resStr.stringByReplacingOccurrencesOfString_withString(NSStr('60'), NSStr('sixty'));
114+
115+ // Output
116+ NSLog(NSStr('Search string: %@'), srchStr);
117+ NSLog(NSStr('Pattern string: %@'), patnStr);
118+
119+ NSLog(NSStr('Range 1: %@'), NSStringFromRange(i));
120+ NSLog(NSStr('Location: %d'), i.location);
121+ NSLog(NSStr('Length: %d'), i.length);
122+ NSLog(NSStr('Match: %@'), srchStr.substringWithRange(i));
123+
124+ NSLog(NSStr('Range 2: %@'), NSStringFromRange(j));
125+ NSLog(NSStr('Location: %d'), j.location);
126+ NSLog(NSStr('Length: %d'), j.length);
127+ NSLog(NSStr('Match: %@'), srchStr.substringWithRange(j));
128+
129+ NSLog(NSStr('Result: %@'), resStr);
130+End.
131+```
132+
133+### Output
134+
135+`2021-06-16 11:41:24.025 regex_ex2[5761:21687] Search string: I have 43 bags of 60 marbles.`\
136+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Pattern string: \d+`\
137+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Range 1: {7, 2}`\
138+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Location: 7`\
139+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Length: 2`\
140+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Match: 43`\
141+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Range 2: {18, 2}`\
142+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Location: 18`\
143+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Length: 2`\
144+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Match: 60`\
145+`2021-06-16 11:41:24.026 regex_ex2[5761:21687] Result: I have forty-three bags of sixty marbles.`
146+
147+## See also
148+
149+- [[macOS NSRegularExpression]].
150+- [[RegEx packages]] - cross platform.
151+
152+## External links
153+
154+- [Apple: NSString](https://developer.apple.com/documentation/foundation/nsstring).
155+- [Apple: rangeOfString:options:range:](https://developer.apple.com/documentation/foundation/nsstring/1415073-rangeofstring).
156+- [Apple: NSStringCompareOptions](https://developer.apple.com/documentation/foundation/nsstringcompareoptions).
157+- [Apple: NSRegularExpressionSearch](https://developer.apple.com/documentation/foundation/nsstringcompareoptions/nsregularexpressionsearch).
158+- [Apple: stringByReplacingOccurrencesOfString:withString:](https://developer.apple.com/documentation/foundation/nsstring/1412937-stringbyreplacingoccurrencesofst).
159+
160+---
161+*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).*

Back to history