macOS NSRegularExpression

From Lazarus wiki
Revision as of 14:27, 12 June 2021 by Trev (talk | contribs) (→‎Operators: New section)
Jump to navigationJump to search

English (en)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide


Regular expressions

Regular expressions are patterns used to match specified alpha-numeric character combinations in the string data being searched.


Each character in a regular expression (that is, each character in the string describing its pattern) is either a metacharacter, having a special meaning, or a regular character that has a literal meaning.

Light bulb  Note: Under construction.

NSRegularExpression Overview

The NSRegularExpression class has convenience methods for returning all the matches as an array, the total number of matches, the first match, and the range of the first match.

An individual match is represented by an instance of the NSTextCheckingResult class, which carries information about the overall matched range (via its range property), and the range of each individual capture group (via the range_AtIndex method).

NSRegularExpression conforms to the International Components for Unicode (ICU) specification for regular expressions.

Metacharacters

For a comprehensive list of characters used by the NSRegularExpression class that have a special meaning in regular expression patterns, see the ICU listing.

Operators

For a comprehensive list of operators used by the NSRegularExpression class, see the ICU listing.

Code Example

In this fairly trivial and contrived code example, we use the /d metacharacter which matches a decimal digit and the + operator to match a decimal digit one or more times. This pattern /d+ aims to match all the occurrences of numbers in the search string which we then output using NSLog().

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

See also

External links