Class DRegEx

Inherits from:
Object
Declared in:
DRegEx.h

Class Hierarchy

    Object
      |
      +---DRegEx

Class Description

The RegEx class implements methods for using regular expressions. After compiling a regular expressing, it can be used to match and search strings.


    This class uses the Extended POSIX syntax:
     .   Match any char (incl. newline)   *         Match zero or more
     +   Match one or more                ?         Match zero or one
     {c} Match exactly c times            {min,max} Match min..max times
     |   Match alternatives               []        Match one in the list
     [^] Match any except in list         [::]      Match a class in a list
     ()  Group or subexpression
     ^   Match begin of line              $         Match end of line

    Classes: alnum, alpha, blank, cntrl, digit, graph, lower, print, punct,
             space, upper, xdigit

    Examples:
          (aab|zef)   matches aab or zef
          aa(b|z)ef   matches aabef or aazef
          [a-c]       matches a or b or c
          []de]       matches ] or d or e
          [^ad]       match any except a and b
          [[:alpha:]] matches letters (*NOTE*: use ccompile and not icompile)
          [.*]        matches . and *
Example:
#include <stdio.h>
#include "ofc/DRegEx.h"
#include "ofc/DText.h"

int main(int argc, char *argv[])
{
  DRegEx *expr1 = [DRegEx alloc];
  DRegEx *expr2 = [DRegEx new  ];

  int     matched;
  int     index;

  [expr1 init :"a+b"];                 // Init with a regular expression

  matched = [expr1 match :"aaba"];     // Match a string with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String1 did not match the regular expression.\n");
  else
    printf("The first %d characters of string1 matched the regular expression.\n", matched);

  matched = [expr1 match :"baaba"];    // Match another string with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String2 did not match the regular expression.\n");
  else
    printf("The first %d characters of the string2 matched the regular expression.\n", matched);

  matched = [expr1 match :"baab" :1];  // Match a string with offset with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String3 did not match the regular expression.\n");
  else
    printf("The offseted %d characters of the string3 matched the regular expression.\n", matched);

  index = [expr1 search :"zyxab"];     // Search for the regular expression in the string
  if (index == DRE_NO_MATCH)
    printf("The regular expression could not be found in string4.\n");
  else
    printf("The regular expression started at %d in the string.\n", index);


  [expr2 icompile :"a(b+)a"];          // Compile a regular expression, case insensitive

  matched = [expr2 match :"abbba"];    // Match the string with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String5 did not match the regular expression.\n");
  else
  {
    DArray *matches = [expr2 matches :"abbba"]; // Get the matches from last match

    if (matches != nil)
    {
      for (index = 0; index < [matches length]; index++)
      {
        DText *str = [matches get :index];

        printf("Match %d:%s.\n", index, [str cstring]);
      }
      [matches free];
    }
  }

  [expr1 free];                        // Cleanup
  [expr2 free];

  return 0;
}
Last modified:
04-Aug-2008 (DRegEx.h)

Instance Variables

private regex_t _pattern
the compiled pattern
private struct re_registers _regs
the match registers
private int _result
the result of the last match/search
private size_t _length
the length of the last string

Method Index


generated 06-Sep-2008 by ObjcDoc 3.0.0