- Inherits from:
- Object
- Declared in:
- DRegEx.h
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
- Constructors
- - (DRegEx *) init
- Initialise to an empty expression
- Returns:
- the object
- - (DRegEx *) init :(const char *) pattern
- Initialise with a pattern
- Parameters:
- pattern - the (case sensitive) c-string pattern
- Returns:
- the object
- Copy related methods
- - shallowCopy
- Do a shallow copy of the object (not implemented)
- Returns:
- the object
- Destructor
- - free
- Free the expression
- Returns:
- the object
- Compile methods
- - (BOOL) ccompile :(const char *) pattern
- Compile a case sensitive pattern (POSIX Extended syntax)
- Parameters:
- pattern - the c-string pattern
- Returns:
- success
- - (BOOL) icompile :(const char *) pattern
- Compile a case insensitive pattern (POSIX Extended syntax)
- Parameters:
- pattern - the c-string pattern
- Returns:
- success
- Matching and searching methods
- - (int) match :(const char *) str
- Match the string for the pattern
- Parameters:
- str - the string to be matched
- Returns:
- the number of characters matched (or DRE_NO_MATCH, DRE_ERROR)
- - (int) match :(const char *) str :(int) from
- Match the string with an offset for the pattern
- Parameters:
- str - the string to be matched
from - the start in the string for the matching
- Returns:
- the number of characters matched (or DRE_NO_MATCH, DRE_ERROR)
- - (int) match :(const unsigned char *) data :(int) length :(int) from
- Match the data string with an offset for the pattern
- Parameters:
- data - the data string to be matched
length - the length of the data string
from - the start in the data string for the matching
- Returns:
- the number of characters matched (or DRE_NO_MATCH, DRE_ERROR)
- - (int) search :(const char *) str
- Search in a string for the pattern
- Parameters:
- str - the string to be searched
- Returns:
- the offset in the string where the pattern matched (or DRE_NO_MATCH, DRE_ERROR)
- - (int) search :(const unsigned char *) data :(int) length
- Search in a data string for the pattern
- Parameters:
- data - the data string to be searched
length - the length of the data string
- Returns:
- the offset in the data string where the pattern matched (or DRE_NO_MATCH, DRE_ERROR)
- - (int) search :(const char *) str :(int) from :(int) to
- Search in a range of a string for the pattern
- Parameters:
- str - the string to be searched
from - the start of the range for searching
to - the end of the range for searching
- Returns:
- the offset in the string where the pattern matched (or DRE_NO_MATCH, DRE_ERROR)
- - (int) search :(const unsigned char *) str :(int) length :(int) from :(int) to
- Search in a range of a data string for the pattern
- Parameters:
- data - the data string to be searched
length - the length of the data string
from - the start of the range for searching
to - the end of the range for searching
- Returns:
- the offset in the data string where the pattern matched (or DRE_NO_MATCH, DRE_ERROR)
- Result methods
- - (DArray *) indices
- Return the subexpression indices from the last match/search. The indices
are returned in a DArray with (new) DInt objects. The sequence in the array
is: index 0 = start of whole matched string, index 1 = end of whole matched
string, index 2 = start of first subexpression, index 3 = end of first
subexpression, and so on. Warning: if a group matches more than once as a
result of a repetition operator, only the last match is stored in the indices.
- Returns:
- a (new) array (or nil for no match)
- - (DArray *) matches :(const char *) str
- Return the matches from the last match/search. The matches are
returned in a (new) DArray with (new) DText objects.
- Parameters:
- str - the string that was matched/searched
- Returns:
- a (new) array (or nil for no match)
- - (DArray *) matches :(const unsigned char *) data :(int) length
- Return the matches from the last match/search. The matches
are returned in a (new) DArray with (new) DData objects.
- Parameters:
- data - the data string that was matched/searched
length - the length of the data string
- Returns:
- a (new) array (or nil for no match)
generated 06-Sep-2008 by ObjcDoc 3.0.0