64 lines
1.6 KiB
C++

#include <oniguruma.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[])
{
static OnigEncoding encodings[] = {
ONIG_ENCODING_ASCII,
ONIG_ENCODING_UTF8,
ONIG_ENCODING_UTF16_BE,
ONIG_ENCODING_UTF16_LE,
ONIG_ENCODING_UTF32_BE,
ONIG_ENCODING_UTF32_LE,
};
int rc = onig_initialize(encodings, sizeof(encodings) / sizeof(encodings[0]));
if (rc != ONIG_NORMAL)
{
OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str(s, rc, nullptr);
fprintf(stderr, "Error: %s\n", s);
return 1;
}
static const char pattern[] = "\\bFoo\\d+";
OnigErrorInfo einfo;
regex_t * regex;
rc = onig_new(&regex, (const OnigUChar *)pattern,
(const OnigUChar *)(pattern + strlen(pattern)), ONIG_OPTION_DEFAULT,
ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo);
if (rc != ONIG_NORMAL)
{
OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str(s, rc, &einfo);
fprintf(stderr, "Error: %s\n", s);
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);
return 0;
}