MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / AR_MATCHREGEX

Function AR_MATCHREGEX

src/arithmetic/string_funcs/string_funcs.c:320–367  ·  view source on GitHub ↗

given a string and a regular expression, return an array of all matches and matching regions string.matchRegEx(str, regex) -> array(array(string))

Source from the content-addressed store, hash-verified

318// return an array of all matches and matching regions
319// string.matchRegEx(str, regex) -> array(array(string))
320SIValue AR_MATCHREGEX(SIValue *argv, int argc, void *private_data) {
321 SIValue list = SIArray_New(0);
322 if(SI_TYPE(argv[0]) == T_NULL || SI_TYPE(argv[1]) == T_NULL) {
323 return list;
324 }
325
326 regex_t *regex;
327 OnigErrorInfo einfo;
328 OnigRegion *region = onig_region_new();
329 const char *str = argv[0].stringval;
330 const char *regex_str = argv[1].stringval;
331
332 int rv = onig_new(&regex, (const UChar *)regex_str,
333 (const UChar *)(regex_str + strlen(regex_str)), ONIG_OPTION_DEFAULT,
334 ONIG_ENCODING_UTF8, ONIG_SYNTAX_JAVA, &einfo);
335 if(rv != ONIG_NORMAL) {
336 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
337 onig_error_code_to_str((UChar* )s, rv, &einfo);
338 ErrorCtx_SetError("Invalid regex, err=%s", s);
339 onig_free(regex);
340 onig_region_free(region, 1);
341 SIValue_Free(list);
342 return SI_NullVal();
343 }
344
345 match_regex_scan_cb_args args = {
346 .list = &list,
347 .str = str
348 };
349
350 rv = onig_scan(regex, (const UChar *)str,
351 (const UChar *)(str + strlen(str)), region, ONIG_OPTION_DEFAULT,
352 match_regex_scan_cb, &args);
353 if(rv < 0) {
354 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
355 onig_error_code_to_str((OnigUChar* )s, rv);
356 ErrorCtx_SetError("Invalid regex, err=%s", s);
357 onig_free(regex);
358 onig_region_free(region, 1);
359 SIValue_Free(list);
360 return SI_NullVal();
361 }
362
363 onig_free(regex);
364 onig_region_free(region, 1);
365
366 return list;
367}
368
369typedef struct {
370 char *res;

Callers

nothing calls this directly

Calls 4

SIArray_NewFunction · 0.85
ErrorCtx_SetErrorFunction · 0.85
SIValue_FreeFunction · 0.85
SI_NullValFunction · 0.85

Tested by

no test coverage detected