Sign in Join
Wiki copytemporary

macOS NSString Regular Expressions: what changed

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

OldNewLine
11## Regular expressions
22
33Regular expressions are patterns used to match specified alpha-numeric character combinations in the string data being searched.
44
55Each 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.
66
77Regular 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/).
88
99## NSString Regular Expression Overview
1010
1111It 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.
1212
1313### Metacharacters
1414
1515For 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).
1616
1717### Operators
1818
1919For a comprehensive list of regular expression operators, see the [ICU listing](https://unicode-org.github.io/icu/userguide/strings/regexp.html#regular-expression-operators).
2020
2121## Example 1 - match a pattern
2222
2323### Code
2424
2525``` pascal
2626Program regex_ex1;
2727
2828{$mode objfpc}{$H+}
2929{$modeswitch objectivec2}
3030
3131Uses
3232 MacOSAll, CocoaAll, SysUtils;
3333
3434Var
3535 srchStr : String;
3636 patnStr : String;
3737 i: NSRange;
3838 j: NSRange;
3939
4040Begin
4141 srchStr := 'I have 43 bags of 60 marbles.';
4242 patnStr := '\d+';
4343
4444 // Find first match
4545 i := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(0, srchStr.Length));
4646
4747 // Find second match
4848 j := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length)));
4949
5050 // Output
5151 NSLog(NSStr('Search string: %@'), NSStr(srchStr));
5252 NSLog(NSStr('Pattern string: %@'), NSStr(patnStr));
5353
5454 NSLog(NSStr('Range 1: %@'), NSStringFromRange(i));
5555 NSLog(NSStr('Location: %d'), i.location);
5656 NSLog(NSStr('Length: %d'), i.length);
5757 NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(i));
5858
5959 NSLog(NSStr('Range 2: %@'), NSStringFromRange(j));
6060 NSLog(NSStr('Location: %d'), j.location);
6161 NSLog(NSStr('Length: %d'), j.length);
6262 NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(j));
6363End.
6464```
6565
6666### Output
6767
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`
68+```text
69+2021-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
79+```
7880
7981## Example 2 - replace matched pattern
8082
8183### Code
8284
8385``` pascal
8486Program regex_ex2;
8587
8688{$mode objfpc}{$H+}
8789{$modeswitch objectivec2}
8890
8991Uses
9092 MacOSAll, CocoaAll, SysUtils;
9193
9294Var
9395 srchStr : NSString;
9496 patnStr : NSString;
9597 resStr : NSString;
9698 i: NSRange;
9799 j: NSRange;
98100
99101Begin
100102 srchStr := NSStr('I have 43 bags of 60 marbles.');
101103 patnStr := NSStr('\d+');
102104
103105 // Find first match
104106 i := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(0, srchStr.Length));
105107
106108 // Do first replacement
107109 resStr := srchStr.stringByReplacingOccurrencesOfString_withString(NSStr('43'), NSStr('forty-three'));
108110
109111 // Find second match
110112 j := srchStr.rangeOfString_options_range(patnStr, NSRegularExpressionSearch, NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length)));
111113
112114 // Do second replacement
113115 resStr := resStr.stringByReplacingOccurrencesOfString_withString(NSStr('60'), NSStr('sixty'));
114116
115117 // Output
116118 NSLog(NSStr('Search string: %@'), srchStr);
117119 NSLog(NSStr('Pattern string: %@'), patnStr);
118120
119121 NSLog(NSStr('Range 1: %@'), NSStringFromRange(i));
120122 NSLog(NSStr('Location: %d'), i.location);
121123 NSLog(NSStr('Length: %d'), i.length);
122124 NSLog(NSStr('Match: %@'), srchStr.substringWithRange(i));
123125
124126 NSLog(NSStr('Range 2: %@'), NSStringFromRange(j));
125127 NSLog(NSStr('Location: %d'), j.location);
126128 NSLog(NSStr('Length: %d'), j.length);
127129 NSLog(NSStr('Match: %@'), srchStr.substringWithRange(j));
128130
129131 NSLog(NSStr('Result: %@'), resStr);
130132End.
131133```
132134
133135### Output
134136
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.`
137+```text
138+2021-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.
149+```
146150
147151## See also
148152
149153- [[macOS NSRegularExpression]].
150154- [[RegEx packages]] - cross platform.
151155
152156## External links
153157
154158- [Apple: NSString](https://developer.apple.com/documentation/foundation/nsstring).
155159- [Apple: rangeOfString:options:range:](https://developer.apple.com/documentation/foundation/nsstring/1415073-rangeofstring).
156160- [Apple: NSStringCompareOptions](https://developer.apple.com/documentation/foundation/nsstringcompareoptions).
157161- [Apple: NSRegularExpressionSearch](https://developer.apple.com/documentation/foundation/nsstringcompareoptions/nsregularexpressionsearch).
158162- [Apple: stringByReplacingOccurrencesOfString:withString:](https://developer.apple.com/documentation/foundation/nsstring/1412937-stringbyreplacingoccurrencesofst).
159163
160164---
161165*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