PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/regex-example2.c

https://bitbucket.org/adrianswtam/junkcode
C | 107 lines | 76 code | 9 blank | 22 comment | 8 complexity | c3df0a969d69fca233b74c304d2917f4 MD5 | raw file
  1. /* regex-example2.c
  2. * Tue, 11 May 2010 12:35:47 -0400
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <regex.h>
  9. /* Example on how to use POSIX Regex calls
  10. * The regex calls supported in *nix can be those provided by GNU, from BSD, or
  11. * as in POSIX standard.
  12. *
  13. * Here the POSIX version is used and the following are the API:
  14. * int regcomp(regex_t *preg, const char *regex, int cflags);
  15. * int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
  16. * size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
  17. * void regfree(regex_t *preg);
  18. */
  19. int main (int argc, char** argv)
  20. {
  21. /* First example: Match a string multiple time */
  22. char* string = " 0.1234 1.2345 2.34567 8 9 10.111213";
  23. char* pstr = string;
  24. int errcode;
  25. regex_t *preg = (regex_t*)malloc(sizeof(regex_t));
  26. regmatch_t *pmatch;
  27. /* Compile regex */
  28. if ((errcode=regcomp(preg, "([0-9]+\\.[0-9]+)",REG_EXTENDED|REG_NEWLINE))) {
  29. char* errbuf;
  30. size_t errbuf_size;
  31. errbuf_size = regerror(errcode, preg, NULL, 0);
  32. if (!(errbuf=(char*)malloc(errbuf_size))) {
  33. perror("malloc error!");
  34. exit(255);
  35. };
  36. regerror(errcode, preg, errbuf, errbuf_size);
  37. fprintf(stderr, "Regex compilation error: %s\n", errbuf);
  38. exit(1);
  39. };
  40. /* Allocate substrings buffer (1 substring) */
  41. pmatch = (regmatch_t*)malloc(sizeof(regmatch_t)*preg->re_nsub);
  42. /* Match regex to string */
  43. if(regexec(preg, pstr, preg->re_nsub, pmatch, 0)) {
  44. printf("Nothing matched with ""%s""\n",pstr);
  45. exit(0);
  46. };
  47. /* Loop to print all the matches */
  48. do {
  49. if (pmatch[0].rm_so != -1) { /* The regex is matching part of a string */
  50. char *submatch;
  51. size_t matchlen = pmatch[0].rm_eo - pmatch[0].rm_so;
  52. submatch = (char*)malloc(matchlen+1);
  53. strncpy(submatch, pstr+pmatch[0].rm_so, matchlen+1);
  54. submatch[matchlen]='\0';
  55. printf("%s;\n", submatch);
  56. free(submatch);
  57. };
  58. pstr += pmatch[0].rm_eo; /* Restart from last match */
  59. } while(!regexec(preg,pstr,preg->re_nsub,pmatch,0));
  60. regfree(preg);
  61. free(pmatch);
  62. /* Second example: Match the whole string once and extract multiple captures */
  63. string = "I have 20 apples and 34 oranges.";
  64. pstr = string;
  65. regcomp(preg, "([0-9]+) apples.* ([0-9]+) oranges",REG_EXTENDED|REG_NEWLINE);
  66. pmatch = (regmatch_t*)malloc(sizeof(regmatch_t)*(1+preg->re_nsub));
  67. printf("There can be %d captures\n",(int)preg->re_nsub);
  68. if(regexec(preg, pstr, 1+preg->re_nsub, pmatch, 0)) {
  69. printf("Nothing matched with ""%s""\n",pstr);
  70. exit(0);
  71. };
  72. if (pmatch[0].rm_so != -1) { /* Regex matched */
  73. /* The whole matched part */
  74. char *submatch;
  75. size_t matchlen = pmatch[0].rm_eo - pmatch[0].rm_so;
  76. submatch = (char*)malloc(matchlen+1);
  77. strncpy(submatch, pstr+pmatch[0].rm_so, matchlen+1);
  78. submatch[matchlen]='\0';
  79. printf("Matched part = %s;\n", submatch);
  80. free(submatch);
  81. /* First capture bracket */
  82. matchlen = pmatch[1].rm_eo - pmatch[1].rm_so;
  83. submatch = (char*)malloc(matchlen+1);
  84. strncpy(submatch, pstr+pmatch[1].rm_so, matchlen+1);
  85. submatch[matchlen]='\0';
  86. printf("Apple = %s;\n", submatch);
  87. free(submatch);
  88. /* Second capture bracket */
  89. matchlen = pmatch[2].rm_eo - pmatch[2].rm_so;
  90. submatch = (char*)malloc(matchlen+1);
  91. strncpy(submatch, pstr+pmatch[2].rm_so, matchlen+1);
  92. submatch[matchlen]='\0';
  93. printf("Orange = %s;\n", submatch);
  94. free(submatch);
  95. };
  96. free(preg);
  97. exit(0);
  98. };