1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21: #include <locale.h>
22: #include <stdio.h>
23: #include <string.h>
24: #include <sys/types.h>
25: #include <regex.h>
26:
27:
28: int
29: main (int argc, char *argv[])
30: {
31: regex_t re;
32: regmatch_t mat[10];
33: int i, j, ret = 0;
34: const char *locales[] = { "C", "de_DE.UTF-8" };
35: const char *string = "http://www.regex.com/pattern/matching.html#intro";
36: regmatch_t expect[10] = {
37: { 0, 48 }, { 0, 5 }, { 0, 4 }, { 5, 20 }, { 7, 20 }, { 20, 42 },
38: { -1, -1 }, { -1, -1 }, { 42, 48 }, { 43, 48 } };
39:
40: for (i = 0; i < sizeof (locales) / sizeof (locales[0]); ++i)
41: {
42: if (setlocale (LC_ALL, locales[i]) == NULL)
43: {
44: puts ("cannot set locale");
45: ret = 1;
46: }
47: else if (regcomp (&re,
48: "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
49: REG_EXTENDED) != REG_NOERROR)
50: {
51: puts ("cannot compile the regular expression");
52: ret = 1;
53: }
54: else if (regexec (&re, string, 10, mat, 0) == REG_NOMATCH)
55: {
56: puts ("no match");
57: ret = 1;
58: }
59: else
60: {
61: if (! memcmp (mat, expect, sizeof (mat)))
62: printf ("matching ok for %s locale\n", locales[i]);
63: else
64: {
65: printf ("matching failed for %s locale:\n", locales[i]);
66: ret = 1;
67: for (j = 0; j < 9; ++j)
68: if (mat[j].rm_so != -1)
69: printf ("%d: %.*s\n", j, mat[j].rm_eo - mat[j].rm_so,
70: string + mat[j].rm_so);
71: }
72: }
73: }
74:
75: return ret;
76: }