/*! \brief Match pattern against string and, if match succeeds, and replace string * with replacement substituting tokens \\d with matched substrings. */ int reg_replace(char *pattern, char *replacement, char *string, str *result) { regmatch_t pmatch[MAX_MATCH]; LM_DBG("pattern: '%s', replacement: '%s', string: '%s'\n", pattern, replacement, string); if (reg_match(pattern, string, &(pmatch[0]))) { return -1; } return replace(&pmatch[0], string, replacement, result); }
/* * Find process by its command line. * 't' is type of operation: * 0 - search below cursor only * 1 - below but including cursor line * 2 - from the beginning */ static int getprocbyname(struct wdgt *w, int t) { struct process *p; char *tmp, buf[8]; int l = w->crsr; for(p = begin; p ; p = p->next){ if(!p->proc) continue; if(!t && p->line <= l) continue; if(t == 1 && p->line < l) continue; /* try the pid first */ snprintf(buf, sizeof buf, "%d", p->proc->pid); if(reg_match(buf)) goto found; //return p->line; /* next process owner */ if(show_owner && reg_match(get_owner_name(p->uid))) goto found; //return p->line; tmp = get_cmdline(p->proc->pid); if(reg_match(tmp)) goto found; // return p->line; } return 2; found: scr_crsr_jmp(w, p->line); return 1; }
int main(int argc, char const *argv[]){ assert(argc>=3); const char* rule = argv[1]; const char* source = argv[2]; printf("rule: %s\n", rule); printf("source: %s\n", source); struct reg_env* env = reg_open_env(); struct reg_pattern* pattern = reg_new_pattern(env, rule); int success = reg_match(pattern, source, strlen(source)); printf("-------------- reslut -----------\n success: %d\n", success); reg_close_env(env); return 0; }
int str_split_ere(const char *str, size_t slen, const char *fmt, size_t flen, ...) { va_list v; char *_str, *_fmt; const char *exp_s, *exp_e; int ignore_flag; char *reg; size_t str_off = 0; size_t off; char *sreq; size_t sreqsize; int ret = 0; if (!str || !slen || !fmt || !flen) return ret; _str = strndup(str, slen); if (!_str) return ret; _fmt = strndup(fmt, flen); if (!_fmt) { free(_str); return ret; } va_start(v, flen); /* supports %[regex..] and %*[regex..] */ exp_s = fmt; while (str_off < flen && *exp_s) { exp_e = exp_end(fmt, flen, exp_s); if (!exp_e) { LOGE("invalid exp - failed to find the end of exp\n"); goto out; } if (exp2reg(exp_s, exp_e, &ignore_flag, ®) == -1) { LOGE("failed to translate exp to reg\n"); goto out; } if (ignore_flag == 1) { sreq = NULL; sreqsize = 0; } else { sreq = va_arg(v, char *); sreqsize = va_arg(v, size_t); } if (reg_match(str + str_off, reg, sreq, sreqsize, &off) == -1) { LOGE("failed to match reg\n"); free(reg); goto out; } else { if (ignore_flag == 0) ret++; } exp_s = exp_e; str_off += off; free(reg); } out: va_end(v); free(_str); free(_fmt); return ret; }