search and display match offset

This commit is contained in:
Josh Holtrop 2018-03-16 19:37:43 -04:00
parent 19cff62a01
commit 5dbf8fc33f

26
main.cc
View File

@ -21,11 +21,11 @@ int main(int argc, char * argv[])
return 1; return 1;
} }
static const char pattern[] = "\\<Foo\\d"; static const char pattern[] = "\\bFoo\\d+";
OnigErrorInfo einfo; OnigErrorInfo einfo;
regex_t * regex; regex_t * regex;
rc = onig_new(&regex, (const OnigUChar *)pattern, rc = onig_new(&regex, (const OnigUChar *)pattern,
(const OnigUChar *)(pattern + strlen(pattern)), ONIG_OPTION_NONE, (const OnigUChar *)(pattern + strlen(pattern)), ONIG_OPTION_DEFAULT,
ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo); ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo);
if (rc != ONIG_NORMAL) if (rc != ONIG_NORMAL)
{ {
@ -35,6 +35,28 @@ int main(int argc, char * argv[])
return 1; return 1;
} }
static const char data[] = "Hi Foo! BarFoo4! Foo42!";
int pos = onig_search(regex,
(OnigUChar *)data,
(OnigUChar *)(data + strlen(data)),
(OnigUChar *)data,
(OnigUChar *)(data + strlen(data)),
NULL, ONIG_OPTION_NONE);
if (pos >= 0)
{
fprintf(stderr, "Match at offset %d\n", pos);
}
else if (pos == ONIG_MISMATCH)
{
fprintf(stderr, "No match!\n");
}
else
{
fprintf(stderr, "Error!\n");
}
onig_free(regex); onig_free(regex);
return 0; return 0;