codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <stdio.h> #include <regex.h> int regex_match (char *pattern, const char *string, size_t sub_size, char **sub) { regmatch_t match[sub_size]; size_t n; size_t m; size_t d; int status; regex_t re; status = regcomp (&re, pattern, REG_EXTENDED); if (status != 0) { return (status); } status = regexec (&re, string, sub_size, match, 0); if (status == 0) { for (n=0; n < sub_size; n++) { for (d = 0, m = match[n].rm_so; m >= 0 && m < match[n].rm_eo; m++, d++) { sub[n][d] = string[m]; } sub[n][d] = '\0'; } } regfree (&re); return (status); } int main (void) { int result; char *string = "Ciao amore mio"; char *re = "Ciao (amo)re"; char sub0[200]; sub0[0] = '\0'; char sub1[200]; sub1[0] = '\0'; char *sub[] = {sub0, sub1}; result = regex_match (re, string, 2, sub); if (result == 0) { printf ("Il modello \"%s\" trova corrispondenza ", re); printf ("nella stringa \"%s\", precisamente ", string); printf ("nella porzione \"%s\", mentre la ", sub[0]); printf ("sottostringa estratta รจ \"%s\".\n", sub[1]); } else { printf ("Il modello \"%s\" ", re); printf ("NON trova corrispondenza "); printf ("nella stringa \"%s\"\n", string); } return 0; }
Private
[
?
]
Run code
Submit