14 #ifdef BUILDING_ECERE_COM
19 public import static "ecere"
25 public class Regex : struct
32 if((value || regex) && (value != regex || strcmp(value, regex)))
37 regex = CopyString(value);
48 property bool caseInsensitive
52 if(value != ((compileFlags & REG_ICASE) != 0))
54 value ? (compileFlags |= REG_ICASE) : (compileFlags &= ~REG_ICASE);
59 get { return (compileFlags & REG_ICASE) != 0; }
62 property bool newLineException
66 //if(value != ((compileFlags & REG_NEWLINE) != 0))
67 if(value != ((compileFlags & REG_EXTENDED)))
69 value ? (compileFlags |= REG_NEWLINE) : (compileFlags &= ~REG_NEWLINE);
74 get { return (compileFlags & REG_NEWLINE) != 0; }
77 property bool lineStartException
81 if(value != ((executeFlags & REG_NOTBOL) != 0))
82 value ? (executeFlags |= REG_NOTBOL) : (executeFlags &= ~REG_NOTBOL);
84 get { return (executeFlags & REG_NOTBOL) != 0; }
87 property bool lineEndException
91 if(value != ((executeFlags & REG_NOTEOL) != 0))
92 value ? (executeFlags |= executeFlags | REG_NOTEOL) : (executeFlags &= ~REG_NOTEOL);
94 get { return (executeFlags & REG_NOTEOL) != 0; }
97 property bool extendedSyntax
101 if(value != ((compileFlags & REG_EXTENDED) != 0))
103 value ? (compileFlags |= REG_EXTENDED) : (compileFlags &= ~REG_EXTENDED);
108 get { return (compileFlags & REG_EXTENDED) != 0; }
111 property bool valid { get { return valid; } }
113 property int maxMatchCount
117 if(value != maxMatchCount)
119 maxMatchCount = value > 0 ? value : 1;
121 matches = new regmatch_t[maxMatchCount];
124 get { return maxMatchCount; }
127 property int matchCount { get { return matchCount; } }
129 char * Match(const String string)
135 result = regexec(&compiledRegex, string, maxMatchCount, matches, executeFlags);
136 if(result == 0) // != REG_NOMATCH
138 for(c = 0; c < maxMatchCount; c++)
140 if(matches[c].rm_so == -1)
146 if(c == maxMatchCount)
147 matchCount = maxMatchCount;
148 return (char *)string + matches[0].rm_so;
156 int GetMatchStartOffset(int matchPos)
158 return matches[matchPos].rm_so;
161 int GetMatchEndOffset(int matchPos)
163 return matches[matchPos].rm_eo;
172 regex_t compiledRegex;
173 regmatch_t * matches;
184 regfree(&compiledRegex);
191 regfree(&compiledRegex);
192 // compileFlags -- REG_NOSUB -- no substring addressing of matches -- nmatch and pmatch in regexec(...) are ignored if used
193 result = regcomp(&compiledRegex, regex, compileFlags/* | REG_NOSUB*/);
195 if(valid && !maxMatchCount)
196 property::maxMatchCount = 1;
198 // TODO: handle errors?
199 // size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
200 // REG_BADBR Invalid use of back reference operator.
201 // REG_BADPAT Invalid use of pattern operators such as group or list.
202 // REG_BADRPT Invalid use of repetition operators such as using '*' as the first character.
203 // REG_EBRACE Un-matched brace interval operators.
204 // REG_EBRACK Un-matched bracket list operators.
205 // REG_ECOLLATE Invalid collating element.
206 // REG_ECTYPE Unknown character class name.
207 // REG_EEND Non specific error. This is not defined by POSIX.2.
208 // REG_EESCAPE Trailing backslash.
209 // REG_EPAREN Un-matched parenthesis group operators.
210 // REG_ERANGE Invalid use of the range operator, eg. the ending point of the range occurs prior to the starting point.
211 // REG_ESIZE Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2.
212 // REG_ESPACE The regex routines ran out of memory.
213 // REG_ESUBREG Invalid back reference to a subexpression.