/regex-example.c
C | 65 lines | 44 code | 7 blank | 14 comment | 5 complexity | 25acf70a912bdcddb34ad82eede25eed MD5 | raw file
- #include <stdio.h>
- #include <sys/types.h>
- #include <regex.h>
- /* Example on how to use POSIX Regex calls
- * The regex calls supported in *nix can be those provided by GNU, from BSD, or
- * as in POSIX standard.
- *
- * Here the POSIX version is used and the following are the API:
- * int regcomp(regex_t *preg, const char *regex, int cflags);
- * int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
- * size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
- * void regfree(regex_t *preg);
- */
- int main (int argc, char** argv)
- {
- char* string = " 0.1234 1.2345 2.34567 8 9 10.111213";
- char* pstr = string;
- int errcode;
- regex_t *preg = (regex_t*)malloc(sizeof(regex_t));
- regmatch_t *pmatch;
- /* Compile regex */
- if ((errcode=regcomp(preg, "([0-9]+\\.[0-9]+)",REG_EXTENDED|REG_NEWLINE))) {
- char* errbuf;
- size_t errbuf_size;
- errbuf_size = regerror(errcode, preg, NULL, 0);
- if (!(errbuf=(char*)malloc(errbuf_size))) {
- perror("malloc error!");
- exit(255);
- };
- regerror(errcode, preg, errbuf, errbuf_size);
- fprintf(stderr, "Regex compilation error: %s\n", errbuf);
- exit(1);
- };
- /* Allocate substrings buffer (1 substring) */
- pmatch = (regmatch_t*)malloc(sizeof(regmatch_t)*preg->re_nsub);
- /* Match regex to string */
- if(regexec(preg, pstr, preg->re_nsub, pmatch, 0)) {
- printf("Nothing matched with ""%s""\n",pstr);
- exit(0);
- };
-
- /* Loop to print all the matches */
- do {
- if (pmatch[0].rm_so != -1) { /* The only bracket of regex is matched */
- char *submatch;
- size_t matchlen = pmatch[0].rm_eo - pmatch[0].rm_so;
- submatch = (char*)malloc(matchlen+1);
- strncpy(submatch, pstr+pmatch[0].rm_so, matchlen+1);
- submatch[matchlen]='\0';
- printf("%s;\n", submatch);
- free(submatch);
- };
- pstr += pmatch[0].rm_eo;
- } while(!regexec(preg,pstr,preg->re_nsub,pmatch,0));
- regfree(preg);
- free(pmatch);
- free(preg);
- exit(0);
- };