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))
54 compileFlags = value ? compileFlags | REG_ICASE : compileFlags & ~REG_ICASE;
59 get { return (compileFlags & REG_ICASE) != 0; }
62 property bool newLineException
66 if(value != (compileFlags & REG_NEWLINE))
67 compileFlags = value ? compileFlags | REG_NEWLINE : compileFlags & ~REG_NEWLINE;
71 get { return (compileFlags & REG_NEWLINE) != 0; }
74 property bool lineStartException
78 if(value != (executeFlags & REG_NOTBOL))
79 executeFlags = value ? executeFlags | REG_NOTBOL : executeFlags & ~REG_NOTBOL;
81 get { return (executeFlags & REG_NOTBOL) != 0; }
84 property bool lineEndException
88 if(value != (executeFlags & REG_NOTEOL))
89 executeFlags = value ? executeFlags | REG_NOTEOL : executeFlags & ~REG_NOTEOL;
91 get { return (executeFlags & REG_NOTEOL) != 0; }
94 property bool extendedSyntax
98 if(value != (compileFlags & REG_EXTENDED))
100 compileFlags = value ? compileFlags | REG_EXTENDED : compileFlags & ~REG_EXTENDED;
105 get { return (compileFlags & REG_EXTENDED) != 0; }
108 property bool valid { get { return valid; } }
110 property int maxMatchCount
114 if(value != maxMatchCount)
116 maxMatchCount = value > 0 ? value : 1;
118 matches = new regmatch_t[maxMatchCount];
121 get { return maxMatchCount; }
124 property int matchCount { get { return matchCount; } }
126 char * Match(String string)
132 result = regexec(&compiledRegex, string, maxMatchCount, matches, executeFlags);
133 if(result == 0) // != REG_NOMATCH
135 for(c = 0; c < maxMatchCount; c++)
137 if(matches[c].rm_so == -1)
143 if(c == maxMatchCount)
144 matchCount = maxMatchCount;
145 return string + matches[0].rm_so;
153 int GetMatchStartOffset(int matchPos)
155 return matches[matchPos].rm_so;
158 int GetMatchEndOffset(int matchPos)
160 return matches[matchPos].rm_eo;
169 regex_t compiledRegex;
170 regmatch_t * matches;
181 regfree(&compiledRegex);
188 regfree(&compiledRegex);
189 // compileFlags -- REG_NOSUB -- no substring addressing of matches -- nmatch and pmatch in regexec(...) are ignored if used
190 result = regcomp(&compiledRegex, regex, compileFlags/* | REG_NOSUB*/);
192 if(valid && !maxMatchCount)
193 property::maxMatchCount = 1;
195 // TODO: handle errors?
196 // size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
197 // REG_BADBR Invalid use of back reference operator.
198 // REG_BADPAT Invalid use of pattern operators such as group or list.
199 // REG_BADRPT Invalid use of repetition operators such as using '*' as the first character.
200 // REG_EBRACE Un-matched brace interval operators.
201 // REG_EBRACK Un-matched bracket list operators.
202 // REG_ECOLLATE Invalid collating element.
203 // REG_ECTYPE Unknown character class name.
204 // REG_EEND Non specific error. This is not defined by POSIX.2.
205 // REG_EESCAPE Trailing backslash.
206 // REG_EPAREN Un-matched parenthesis group operators.
207 // REG_ERANGE Invalid use of the range operator, eg. the ending point of the range occurs prior to the starting point.
208 // REG_ESIZE Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2.
209 // REG_ESPACE The regex routines ran out of memory.
210 // REG_ESUBREG Invalid back reference to a subexpression.