#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <regex.h>
#define ERRBUF_SIZE 1024
#define NMATCH 10
void error(int err_code, regex_t *rs)1
{
char errbuf[ERRBUF_SIZE];
if(!err_code) return;
regerror(err_code, rs, errbuf, ERRBUF_SIZE);
fprintf(stderr, "regex error: %s\n", errbuf);
regfree(rs);
_exit(err_code);
}
int main(void)
{
const char str[] = "тестов\xD1\xD1ая строчка. ABCDEFG";2
const char regex[] = ".*";3
regex_t rs;
regmatch_t pmatch[NMATCH];
char *lc = setlocale(LC_ALL, "ru_RU.UTF-8");4
if(!lc) return 77;
printf("test string: `%s' (%s)\n", str, lc);
int err = regcomp(&rs, regex, REG_EXTENDED);
error(err, &rs);
const char *ps = str;
while(*ps)5
{
err = regexec(&rs, ps, NMATCH, pmatch, 0);
error(err, &rs);
int j;
const char *ps2 = NULL;
for(j = 0; j < NMATCH; j++)
{
if(pmatch[j].rm_so < 0)
continue;
printf("Совпадение %d: %d...%d\t`", j, pmatch[j].rm_so, pmatch[j].rm_eo);
int i;
for(i = pmatch[j].rm_so; i < pmatch[j].rm_eo; i++)
printf("%c", ps[i]);
printf("'\n");
if(!ps2)
ps2 = ps + i;6
}
ps = ps2;
if(!ps) break;7
printf("остаток: `%s'\n", ps);
}
regfree(&rs);
return 0;
}
Карусель разнесло по цепочке за час
Всех известий — конец
Да, весна началась!
(всё к лицу подлецу, как родному отцу, не рассказывай, батя, и так всё пройдёт)