Regular Expresions in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Juan Alvarez
    New Member
    • Oct 2006
    • 9

    Regular Expresions in C

    What i need to do to use Regex in C?

    Any help?
  • RADAR
    New Member
    • Oct 2006
    • 21

    #2
    Code:
    #include // sys/types.h
     
    #include // regex.h
     
    
    
     
    int match(const char *string, char *pattern) {
     
      int status;
     
      regex_t re;
     
    
     
      if(regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
     
        return 0; }
     
      status = regexec(&re, string, (size_t)0, NULL, 0);
     
      regfree(&re);
     
      if(status != 0) {
     
        return 0; }
     
      return 1; }
    Also there is a shortcut without the header file
    int error;
    Code:
      regex_t pdata;
      regmatch_t pmatch[3];
    
      if (error = regcomp(&pdata, "a\([bc]*\)a\([bc]*\)a", 0) {
        /* handle error */
      } else if (error = regexec(&pdata, "xxxabcbabbcaxxx", 3, pmatch, 0) {
        /* handle error */
      } else {
        printf("The regex matches from %d to %d.\n",
                pmatch[0].rm_so, pmatch[0].rm_eo);
        regfree(&pdata);
      }
    respects,
    Last edited by RADAR; Oct 25 '06, 02:53 PM. Reason: Adding some different codes

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      I think you will find that these functions are not standard C (or C++) however if your system supports them then use them but remember using them will reduce the portability of your code.

      Comment

      • Juan Alvarez
        New Member
        • Oct 2006
        • 9

        #4
        Thanks for replying =)

        Comment

        Working...