macOS NSRegularExpression
From Lazarus wiki
│ 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
srchStr : String;
patnStr : String;
myRegex : NSregularExpression;
matches : NSArray;
match : NSTextCheckingResult;
error : NSErrorPtr;
Begin
error := Nil;
srchStr := 'I have 43 bags of 60 marbles.';
patnStr := '\d+';
myRegex := NSregularExpression.regularExpressionWithPattern_options_error(NSStr(patnStr), NSRegularExpressionOptions(NSRegularExpressionCaseInsensitive), error);
if(error <> Nil) then
NSLog(NSStr('Error: %@'), error);
matches := myRegex.matchesInString_options_range(NSStr(srchStr), 0, NSMakeRange(0, srchStr.Length));
NSLog(NSStr('Search string: %@'), NSStr(srchStr));
NSLog(NSStr('Pattern string: %@'), NSStr(patnStr));
NSLog(NSStr('Number of matches: %lu'), myRegex.numberOfMatchesInString_options_range(NSStr(srchStr), 0, NSMakeRange(0, srchStr.Length)));
for match in matches do
NSLog(NSStr('match: %@'), NSStr(srchStr).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