Difference between revisions of "macOS NSRegularExpression"
From Lazarus wiki
Jump to navigationJump to search (New macOS content) |
(→Code Example: Add code example output) |
||
Line 41: | Line 41: | ||
End. | End. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | The output from running the above code example is: | ||
+ | |||
+ | 2021-06-12 21:05:46.335 regex_ex1[26138:232243] Search string: I have 43 bags of 60 marbles. | ||
+ | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] Pattern string: \d+ | ||
+ | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] Number of matches: 2 | ||
+ | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] match: 43 | ||
+ | 2021-06-12 21:05:46.336 regex_ex1[26138:232243] match: 60 | ||
== External links == | == External links == | ||
* [https://developer.apple.com/documentation/foundation/nsregularexpression Apple: NSRegularExpression] | * [https://developer.apple.com/documentation/foundation/nsregularexpression Apple: NSRegularExpression] |
Revision as of 13:07, 12 June 2021
│ English (en) │
This article applies to macOS only.
See also: Multiplatform Programming Guide
Regular expressions
T/c
Code Example
Program regex_ex1;
{$mode objfpc}{$H+}
{$modeswitch objectivec2}
Uses
MacOSAll, CocoaAll, SysUtils;
Var
testStr : String;
patnStr : String;
matches : NSArray;
myregex : NSregularExpression;
error : NSErrorPtr;
match : NSTextCheckingResult;
Begin
error := Nil;
testStr := 'I have 43 bags of 60 marbles.';
patnStr := '\d+';
myregex := NSregularExpression.regularExpressionWithPattern_options_error(NSStr(patnStr), NSRegularExpressionOptions(NSRegularExpressionCaseInsensitive), error);
matches := myregex.matchesInString_options_range(NSStr(testStr), 0, NSMakeRange(0, testStr.Length));
NSLog(NSStr('Search string: %@'), NSStr(testStr));
NSLog(NSStr('Pattern string: %@'), NSStr(patnStr));
NSLog(NSStr('Number of matches: %lu'), myregex.numberOfMatchesInString_options_range(NSStr(testStr), 0, NSMakeRange(0, testStr.Length)));
for match in matches do
NSLog(NSStr('match: %@'), NSStr(testStr).substringWithRange(match.rangeAtIndex(0)));
End.
The output from running the above code example is:
2021-06-12 21:05:46.335 regex_ex1[26138:232243] Search string: I have 43 bags of 60 marbles. 2021-06-12 21:05:46.336 regex_ex1[26138:232243] Pattern string: \d+ 2021-06-12 21:05:46.336 regex_ex1[26138:232243] Number of matches: 2 2021-06-12 21:05:46.336 regex_ex1[26138:232243] match: 43 2021-06-12 21:05:46.336 regex_ex1[26138:232243] match: 60