diff options
Diffstat (limited to 'sample/posix.c')
-rw-r--r-- | sample/posix.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/sample/posix.c b/sample/posix.c new file mode 100644 index 0000000..d24ee35 --- /dev/null +++ b/sample/posix.c @@ -0,0 +1,93 @@ +/* + * posix.c + */ +#include <stdio.h> +#include "onigposix.h" + +typedef unsigned char UChar; + +static int x(regex_t* reg, unsigned char* pattern, unsigned char* str) +{ + int r, i; + char buf[200]; + regmatch_t pmatch[20]; + + r = regexec(reg, (char* )str, reg->re_nsub + 1, pmatch, 0); + if (r != 0 && r != REG_NOMATCH) { + regerror(r, reg, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + + if (r == REG_NOMATCH) { + fprintf(stderr, "FAIL: /%s/ '%s'\n", pattern, str); + } + else { + fprintf(stderr, "OK: /%s/ '%s'\n", pattern, str); + for (i = 0; i <= (int )reg->re_nsub; i++) { + fprintf(stderr, "%d: %d-%d\n", i, pmatch[i].rm_so, pmatch[i].rm_eo); + } + } + return 0; +} + +extern int main(int argc, char* argv[]) +{ + int r; + char buf[200]; + regex_t reg; + UChar* pattern; + + /* default syntax (ONIG_SYNTAX_RUBY) */ + pattern = (UChar* )"^a+b{2,7}[c-f]?$|uuu"; + r = regcomp(®, (char* )pattern, REG_EXTENDED); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"aaabbbbd"); + + /* POSIX Basic RE (REG_EXTENDED is not specified.) */ + pattern = (UChar* )"^a+b{2,7}[c-f]?|uuu"; + r = regcomp(®, (char* )pattern, 0); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"a+b{2,7}d?|uuu"); + + /* POSIX Basic RE (REG_EXTENDED is not specified.) */ + pattern = (UChar* )"^a*b\\{2,7\\}\\([c-f]\\)$"; + r = regcomp(®, (char* )pattern, 0); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"aaaabbbbbbd"); + + /* POSIX Extended RE */ + onig_set_default_syntax(ONIG_SYNTAX_POSIX_EXTENDED); + pattern = (UChar* )"^a+b{2,7}[c-f]?)$|uuu"; + r = regcomp(®, (char* )pattern, REG_EXTENDED); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"aaabbbbd)"); + + pattern = (UChar* )"^b."; + r = regcomp(®, (char* )pattern, REG_EXTENDED | REG_NEWLINE); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"a\nb\n"); + + regfree(®); + return 0; +} |