search and display match offset
This commit is contained in:
parent
19cff62a01
commit
5dbf8fc33f
26
main.cc
26
main.cc
@ -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(®ex, (const OnigUChar *)pattern,
|
rc = onig_new(®ex, (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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user