Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
|
FXRex is a regular expression class implementing a NFA matcher. More...
#include <FXRex.h>
Public Types | |
enum | { Normal = 0, Unicode = 1, Syntax = 2, Verbatim = 4, Capture = 8, IgnoreCase = 16, Newline = 32, Exact = 64, NotEmpty = 128, Reverse = 256, Words = 512, NotBol = 1024, NotEol = 2048 } |
Regular expression flags. More... | |
enum | Error { ErrOK = 0, ErrEmpty = 1, ErrMore = 2, ErrParent = 3, ErrBracket = 4, ErrBrace = 5, ErrRange = 6, ErrEscape = 7, ErrCount = 8, ErrNoAtom = 9, ErrRepeat = 10, ErrBackRef = 11, ErrClass = 12, ErrComplex = 13, ErrMemory = 14, ErrToken = 15, ErrLong = 16, ErrSupport = 17 } |
Regular expression error codes. More... | |
Public Member Functions | |
FXRex () | |
Construct empty regular expression object, with the fallback program installed. | |
FXRex (const FXRex &orig) | |
Copy regular expression object from another. | |
FXRex (const FXchar *pattern, FXint mode=Normal, Error *error=nullptr) | |
Compile expression from pattern; if error is not NULL, error code is returned. | |
FXRex (const FXString &pattern, FXint mode=Normal, Error *error=nullptr) | |
Compile expression from pattern; if error is not NULL, error code is returned. | |
FXbool | empty () const |
See if regular expression is empty; the regular expression will be empty when it is unable to parse a pattern due to a syntax error. | |
Error | parse (const FXchar *pattern, FXint mode=Normal) |
Parse pattern, return error code if syntax error is found. More... | |
Error | parse (const FXString &pattern, FXint mode=Normal) |
FXbool | amatch (const FXchar *string, FXint len, FXint pos=0, FXint mode=Normal, FXint *beg=nullptr, FXint *end=nullptr, FXint npar=0) const |
Perform anchored match of subject string of length len at position pos, returning true if the pattern matches at this point. More... | |
FXbool | amatch (const FXString &string, FXint pos=0, FXint mode=Normal, FXint *beg=nullptr, FXint *end=nullptr, FXint npar=0) const |
FXint | search (const FXchar *string, FXint len, FXint fm, FXint to, FXint mode=Normal, FXint *beg=nullptr, FXint *end=nullptr, FXint npar=0) const |
Search subject string of length len for a pattern, returning the location where the pattern is found relative from the start of the string, or -1 if there is no match. More... | |
FXint | search (const FXString &string, FXint fm, FXint to, FXint mode=Normal, FXint *beg=nullptr, FXint *end=nullptr, FXint npar=0) const |
FXRex & | operator= (const FXRex &orig) |
Assign another regular expression to this one. | |
FXbool | operator== (const FXRex &rex) const |
Comparison operators. | |
FXbool | operator!= (const FXRex &rex) const |
void | clear () |
Clear the expression object and reinstate the fallback program. | |
~FXRex () | |
Delete. | |
Static Public Member Functions | |
static FXString | substitute (const FXchar *string, FXint len, FXint *beg, FXint *end, const FXchar *replace, FXint npar=1) |
After performing a regular expression match with capturing parentheses, a substitution string is build from the replace string, where where "&" is replaced by the entire matched pattern, and "\1" through "\9" are replaced by captured expressions. More... | |
static FXString | substitute (const FXchar *string, FXint len, FXint *beg, FXint *end, const FXString &replace, FXint npar=1) |
static FXString | substitute (const FXString &string, FXint *beg, FXint *end, const FXchar *replace, FXint npar=1) |
static FXString | substitute (const FXString &string, FXint *beg, FXint *end, const FXString &replace, FXint npar=1) |
static const FXchar * | getError (Error err) |
Returns error message text for a given error code. | |
static FXString | escape (const FXString &str) |
Escape special regex-reserved characters in string. More... | |
Friends | |
FXAPI FXStream & | operator<< (FXStream &store, const FXRex &s) |
Saving and loading. | |
FXAPI FXStream & | operator>> (FXStream &store, FXRex &s) |
FXRex is a regular expression class implementing a NFA matcher.
It supports capturing parentheses, non-capturing parentheses, positive or negative lookahead, backreferences, case-insensitive matching, counted repetitions, greedy, lazy and possessive matches, and PERL-like matching operators. The subject string may be searched forwards or backwards, and may contain any of 256 possible byte values.
When parsing a regular expression pattern, the mode parameter is the bitwise OR of a set of flags and affects the match algorithm. Passing the flag Capture enables capturing parentheses and back references, and allows the matcher engine to return the locations of the string matching these sub-patterns. The flag IgnoreCase enables case-insensitive matching.
When the flag Newline is passed, newlines are treated like normal characters, and not line-separators. If Newline flag is not passed, character classes such as '.', '', '', [^a-z] etc. will NOT match newlines. The flag Verbatim disables all special character interpretation, making the entire pattern a literal string to be matched against a string.
When the Exact flag is passed, a match succeeds only if the entire string is matched, i.e. the entire input presented to FXRex must match against the pattern; otherwise, only a (possibly empty) substring of the input is matched against the pattern. If the NotEmpty flag is passed, the pattern must match at least one character in order to succeed, and empty matches are considered non-matching.
If the flag Syntax will check the pattern for correct syntax only, and not generate a matching engine; it will just reset the engine to the empty pattern; use this flag to verify the syntax of the pattern without compiling it.
When matching a compiled pattern, the mode parameter is the bitwise OR of a set of flags that affects how the match is performed. Passing the flags NotBol and/or NotEol causes the begin and end of the subject string NOT to be considered a line start or line end.
Patterns which cause inordinate amounts of recursion may cause FXRex to fail where otherwise it would succeed to match. FXRex uses no global variables, and thus multiple threads may simultaneously use it; moreover, multiple threads may use the same instance to perform a match.
anonymous enum |
Regular expression flags.
enum FX::FXRex::Error |
Regular expression error codes.
FXbool FX::FXRex::amatch | ( | const FXchar * | string, |
FXint | len, | ||
FXint | pos = 0 , |
||
FXint | mode = Normal , |
||
FXint * | beg = nullptr , |
||
FXint * | end = nullptr , |
||
FXint | npar = 0 |
||
) | const |
Perform anchored match of subject string of length len at position pos, returning true if the pattern matches at this point.
If there is a match, the pattern and subpatterns are captured in the arrays beg[] and end[] which must both be at least npar entries long.
Escape special regex-reserved characters in string.
Useful when assembling a regular expression from strings.
Parse pattern, return error code if syntax error is found.
The parse-mode flags control the compile options, and affect how the generated matcher behaves. If a parse fails, an error code is returned; in this case, the expression matcher will be set up to a fallback program.
FXint FX::FXRex::search | ( | const FXchar * | string, |
FXint | len, | ||
FXint | fm, | ||
FXint | to, | ||
FXint | mode = Normal , |
||
FXint * | beg = nullptr , |
||
FXint * | end = nullptr , |
||
FXint | npar = 0 |
||
) | const |
Search subject string of length len for a pattern, returning the location where the pattern is found relative from the start of the string, or -1 if there is no match.
In case of a successful match, the pattern and subpatterns are captured in the arrays beg[] and end[] which must be at least npar entries long. The string is searched forwards (or backwards) starting from position fm toward to, both of which must lie inside the string.
|
static |
After performing a regular expression match with capturing parentheses, a substitution string is build from the replace string, where where "&" is replaced by the entire matched pattern, and "\1" through "\9" are replaced by captured expressions.
The original source string and its length, and the match arrays beg and end must be passed. The replace string may also contain regular escape sequences to embed special characters.
Copyright © 1997-2022 Jeroen van der Zijp |