4 * capture history (?@...) sample.
11 node_callback(int group, int beg, int end, int level, int at, void* arg)
15 if (at != ONIG_TRAVERSE_CALLBACK_AT_FIRST)
16 return -1; /* error */
19 for (i = 0; i < level * 2; i++)
22 fprintf(stderr, "%d: (%d-%d)\n", group, beg, end);
26 extern int ex(unsigned char* str, unsigned char* pattern,
27 OnigSyntaxType* syntax)
30 unsigned char *start, *range, *end;
35 r = onig_new(®, pattern, pattern + strlen((char* )pattern),
36 ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, syntax, &einfo);
37 if (r != ONIG_NORMAL) {
38 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
39 onig_error_code_to_str(s, r, &einfo);
40 fprintf(stderr, "ERROR: %s\n", s);
44 fprintf(stderr, "number of captures: %d\n", onig_number_of_captures(reg));
45 fprintf(stderr, "number of capture histories: %d\n",
46 onig_number_of_capture_histories(reg));
48 region = onig_region_new();
50 end = str + strlen((char* )str);
53 r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
57 fprintf(stderr, "match at %d\n", r);
58 for (i = 0; i < region->num_regs; i++) {
59 fprintf(stderr, "%d: (%d-%d)\n", i, region->beg[i], region->end[i]);
61 fprintf(stderr, "\n");
63 r = onig_capture_tree_traverse(region, ONIG_TRAVERSE_CALLBACK_AT_FIRST,
64 node_callback, (void* )0);
66 else if (r == ONIG_MISMATCH) {
67 fprintf(stderr, "search fail\n");
70 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
71 onig_error_code_to_str(s, r);
75 onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
81 extern int main(int argc, char* argv[])
86 static UChar* str1 = (UChar* )"((())())";
87 static UChar* pattern1
88 = (UChar* )"\\g<p>(?@<p>\\(\\g<s>\\)){0}(?@<s>(?:\\g<p>)*|){0}";
90 static UChar* str2 = (UChar* )"x00x00x00";
91 static UChar* pattern2 = (UChar* )"(?@x(?@\\d+))+";
93 static UChar* str3 = (UChar* )"0123";
94 static UChar* pattern3 = (UChar* )"(?@.)(?@.)(?@.)(?@.)";
96 OnigEncoding use_encs[] = { ONIG_ENCODING_ASCII };
97 onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));
99 /* enable capture hostory */
100 onig_copy_syntax(&syn, ONIG_SYNTAX_DEFAULT);
101 onig_set_syntax_op2(&syn,
102 onig_get_syntax_op2(&syn) | ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY);
104 r = ex(str1, pattern1, &syn);
105 r = ex(str2, pattern2, &syn);
106 r = ex(str3, pattern3, &syn);