0

I'm trying to use the TRE library in my C program to perform a fuzzy regex search. I've managed to piece together this code from reading the docs:

regex_t rx;
regcomp(&rx, "(January|February)", REG_EXTENDED);
int result = regexec(&rx, "January", 0, 0, 0);

However, this will match only an exact regex (i.e. no spelling errors are allowed). I don't see any parameter which allows to set the fuzziness in those functions:

int regcomp(regex_t *preg, const char *regex, int cflags); 
int regexec(const regex_t *preg, const char *string, size_t nmatch, 
        regmatch_t pmatch[], int eflags); 

How can I set the level of fuzziness (i.e. maximum Levenshtein distance), and how do I get the Levenshtein distance of the match?

Edit: I forgot to mention I'm using the Windows binaries from GnuWin32, which are available only for version 0.7.5. Binaries for 0.8.0 are available only for Linux.

Community
  • 1
  • 1
sashoalm
  • 63,456
  • 96
  • 348
  • 677
  • See http://laurikari.net/tre/documentation/regaexec/ – Wiktor Stribiżew Feb 04 '16 at 10:55
  • Ok, at first I thought I don't have this function, because it's listed as `tre_regaexec` in the link you gave, but it's listed as only `regaexec` in my `regex.h`. Possibly because my version is 0.7.5 I assume. – sashoalm Feb 04 '16 at 11:04

1 Answers1

2

Thanks to @Wiktor Stribiżew, I found out which function I need to use, and I've successfully compiled a working example:

#include <stdio.h>
#include "regex.h"
int main() {
    regex_t rx;
    regcomp(&rx, "(January|February)", REG_EXTENDED);

    regaparams_t params = { 0 };
    params.cost_ins = 1;
    params.cost_del = 1;
    params.cost_subst = 1;
    params.max_cost = 2;
    params.max_del = 2;
    params.max_ins = 2;
    params.max_subst = 2;
    params.max_err = 2;

    regamatch_t match;
    match.nmatch = 0;
    match.pmatch = 0;

    if (!regaexec(&rx, "Janvary", &match, params, 0)) {
        printf("Levenshtein distance: %d\n", match.cost);
    } else {
        printf("Failed to match\n");
    }

    return 0;
}
sashoalm
  • 63,456
  • 96
  • 348
  • 677