Difference between revisions of "macOS NSString Regular Expressions"
From Free Pascal wiki
Jump to navigationJump to search (New page stub) |
(Add code example, "See also" section, "External links" section) |
||
Line 2: | Line 2: | ||
{{CategoriesBySuffixForPage|macOS|Code Snippets}}</includeonly> | {{CategoriesBySuffixForPage|macOS|Code Snippets}}</includeonly> | ||
− | {{Note|Under | + | {{Note|Under construction}} |
+ | === Code === | ||
+ | |||
+ | <syntaxhighlight lang=pascal> | ||
+ | Program regex_1; | ||
+ | |||
+ | {$mode objfpc}{$H+} | ||
+ | {$modeswitch objectivec2} | ||
+ | |||
+ | Uses | ||
+ | MacOSAll, CocoaAll, SysUtils; | ||
+ | |||
+ | Var | ||
+ | srchStr : String; | ||
+ | patnStr : String; | ||
+ | i: NSRange; | ||
+ | j: NSRange; | ||
+ | |||
+ | Begin | ||
+ | srchStr := 'I have 43 bags of 60 marbles.'; | ||
+ | patnStr := '\d+'; | ||
+ | |||
+ | // Find first match | ||
+ | i := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(0, srchStr.Length)); | ||
+ | |||
+ | // Find second match | ||
+ | j := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length))); | ||
+ | |||
+ | // Output | ||
+ | NSLog(NSStr('Search string: %@'), NSStr(srchStr)); | ||
+ | NSLog(NSStr('Pattern string: %@'), NSStr(patnStr)); | ||
+ | |||
+ | NSLog(NSStr('Range 1: %@'), NSStringFromRange(i)); | ||
+ | NSLog(NSStr('Location: %d'), i.location); | ||
+ | NSLog(NSStr('Length: %d'), i.length); | ||
+ | NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(i)); | ||
+ | |||
+ | NSLog(NSStr('Range 2: %@'), NSStringFromRange(j)); | ||
+ | NSLog(NSStr('Location: %d'), j.location); | ||
+ | NSLog(NSStr('Length: %d'), j.length); | ||
+ | NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(j)); | ||
+ | End. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Output === | ||
+ | |||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Search string: I have 43 bags of 60 marbles. | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Pattern string: \d+ | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Range 1: {7, 2} | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Location: 7 | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Length: 2 | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Match: 43 | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Range 2: {18, 2} | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Location: 18 | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Length: 2 | ||
+ | 2021-06-15 21:43:49.049 strprg1[23182:177970] Match: 60 | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | * [[macOS NSRegularExpression]]. | ||
+ | * [[Regexpr]] - cross platform. | ||
+ | |||
+ | == External links == | ||
+ | |||
+ | * [https://developer.apple.com/documentation/foundation/nsstring Apple: NSString]. | ||
+ | * [https://developer.apple.com/documentation/foundation/nsstring/1415073-rangeofstring Apple: rangeOfString:options:range:]. | ||
+ | * [https://developer.apple.com/documentation/foundation/nsstringcompareoptions Apple: NSStringCompareOptions]. | ||
[[Category:macOS]] | [[Category:macOS]] | ||
[[Category:Code Snippets]] | [[Category:Code Snippets]] |
Revision as of 13:46, 15 June 2021
│ English (en) │
This article applies to macOS only.
See also: Multiplatform Programming Guide
Note: Under construction
Code
Program regex_1;
{$mode objfpc}{$H+}
{$modeswitch objectivec2}
Uses
MacOSAll, CocoaAll, SysUtils;
Var
srchStr : String;
patnStr : String;
i: NSRange;
j: NSRange;
Begin
srchStr := 'I have 43 bags of 60 marbles.';
patnStr := '\d+';
// Find first match
i := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(0, srchStr.Length));
// Find second match
j := NSStr(srchStr).rangeOfString_options_range(NSStr(patnStr), (NSRegularExpressionSearch or NSCaseInsensitiveSearch), NSMakeRange(i.location + i.length, srchStr.Length - (i.location + i.length)));
// Output
NSLog(NSStr('Search string: %@'), NSStr(srchStr));
NSLog(NSStr('Pattern string: %@'), NSStr(patnStr));
NSLog(NSStr('Range 1: %@'), NSStringFromRange(i));
NSLog(NSStr('Location: %d'), i.location);
NSLog(NSStr('Length: %d'), i.length);
NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(i));
NSLog(NSStr('Range 2: %@'), NSStringFromRange(j));
NSLog(NSStr('Location: %d'), j.location);
NSLog(NSStr('Length: %d'), j.length);
NSLog(NSStr('Match: %@'), NSStr(srchStr).substringWithRange(j));
End.
Output
2021-06-15 21:43:49.049 strprg1[23182:177970] Search string: I have 43 bags of 60 marbles. 2021-06-15 21:43:49.049 strprg1[23182:177970] Pattern string: \d+ 2021-06-15 21:43:49.049 strprg1[23182:177970] Range 1: {7, 2} 2021-06-15 21:43:49.049 strprg1[23182:177970] Location: 7 2021-06-15 21:43:49.049 strprg1[23182:177970] Length: 2 2021-06-15 21:43:49.049 strprg1[23182:177970] Match: 43 2021-06-15 21:43:49.049 strprg1[23182:177970] Range 2: {18, 2} 2021-06-15 21:43:49.049 strprg1[23182:177970] Location: 18 2021-06-15 21:43:49.049 strprg1[23182:177970] Length: 2 2021-06-15 21:43:49.049 strprg1[23182:177970] Match: 60
See also
- macOS NSRegularExpression.
- Regexpr - cross platform.