12 #ifdef BUILDING_ECERE_COM
17 public import static "ecere"
23 public class Regex : struct
30 if((value || regex) && (value != regex || strcmp(value, regex)))
35 regex = CopyString(value);
46 property bool caseInsensitive
50 if(value != (compileFlags & REG_ICASE))
52 compileFlags = value ? compileFlags | REG_ICASE : compileFlags & ~REG_ICASE;
57 get { return (compileFlags & REG_ICASE) != 0; }
60 property bool newLineException
64 if(value != (compileFlags & REG_NEWLINE))
65 compileFlags = value ? compileFlags | REG_NEWLINE : compileFlags & ~REG_NEWLINE;
69 get { return (compileFlags & REG_NEWLINE) != 0; }
72 property bool lineStartException
76 if(value != (executeFlags & REG_NOTBOL))
77 executeFlags = value ? executeFlags | REG_NOTBOL : executeFlags & ~REG_NOTBOL;
79 get { return (executeFlags & REG_NOTBOL) != 0; }
82 property bool lineEndException
86 if(value != (executeFlags & REG_NOTEOL))
87 executeFlags = value ? executeFlags | REG_NOTEOL : executeFlags & ~REG_NOTEOL;
89 get { return (executeFlags & REG_NOTEOL) != 0; }
92 property bool extendedSyntax
96 if(value != (compileFlags & REG_EXTENDED))
98 compileFlags = value ? compileFlags | REG_EXTENDED : compileFlags & ~REG_EXTENDED;
103 get { return (compileFlags & REG_EXTENDED) != 0; }
106 property bool valid { get { return valid; } }
108 property int maxMatchCount
112 if(value != maxMatchCount)
114 maxMatchCount = value > 0 ? value : 1;
116 matches = new regmatch_t[maxMatchCount];
119 get { return maxMatchCount; }
122 property int matchCount { get { return matchCount; } }
124 char * Match(String string)
130 result = regexec(&compiledRegex, string, maxMatchCount, matches, executeFlags);
131 if(result == 0) // != REG_NOMATCH
133 for(c = 0; c < maxMatchCount; c++)
135 if(matches[c].rm_so == -1)
141 if(c == maxMatchCount)
142 matchCount = maxMatchCount;
143 return string + matches[0].rm_so;
151 int GetMatchStartOffset(int matchPos)
153 return matches[matchPos].rm_so;
156 int GetMatchEndOffset(int matchPos)
158 return matches[matchPos].rm_eo;
167 regex_t compiledRegex;
168 regmatch_t * matches;
179 regfree(&compiledRegex);
186 regfree(&compiledRegex);
187 // compileFlags -- REG_NOSUB -- no substring addressing of matches -- nmatch and pmatch in regexec(...) are ignored if used
188 result = regcomp(&compiledRegex, regex, compileFlags/* | REG_NOSUB*/);
190 if(valid && !maxMatchCount)
191 property::maxMatchCount = 1;
193 // TODO: handle errors?
194 // size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
195 // REG_BADBR Invalid use of back reference operator.
196 // REG_BADPAT Invalid use of pattern operators such as group or list.
197 // REG_BADRPT Invalid use of repetition operators such as using '*' as the first character.
198 // REG_EBRACE Un-matched brace interval operators.
199 // REG_EBRACK Un-matched bracket list operators.
200 // REG_ECOLLATE Invalid collating element.
201 // REG_ECTYPE Unknown character class name.
202 // REG_EEND Non specific error. This is not defined by POSIX.2.
203 // REG_EESCAPE Trailing backslash.
204 // REG_EPAREN Un-matched parenthesis group operators.
205 // REG_ERANGE Invalid use of the range operator, eg. the ending point of the range occurs prior to the starting point.
206 // REG_ESIZE Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2.
207 // REG_ESPACE The regex routines ran out of memory.
208 // REG_ESUBREG Invalid back reference to a subexpression.