Regular expressions in C: examples?

后端 未结 5 1145
傲寒
傲寒 2020-11-22 04:57

I\'m after some simple examples and best practices of how to use regular expressions in ANSI C. man regex.h does not provide that much help.

5条回答
  •  悲哀的现实
    2020-11-22 05:09

    While the answer above is good, I recommend using PCRE2. This means you can literally use all the regex examples out there now and not have to translate from some ancient regex.

    I made an answer for this already, but I think it can help here too..

    Regex In C To Search For Credit Card Numbers

    // YOU MUST SPECIFY THE UNIT WIDTH BEFORE THE INCLUDE OF THE pcre.h
    
    #define PCRE2_CODE_UNIT_WIDTH 8
    #include 
    #include 
    #include 
    #include 
    
    int main(){
    
    bool Debug = true;
    bool Found = false;
    pcre2_code *re;
    PCRE2_SPTR pattern;
    PCRE2_SPTR subject;
    int errornumber;
    int i;
    int rc;
    PCRE2_SIZE erroroffset;
    PCRE2_SIZE *ovector;
    size_t subject_length;
    pcre2_match_data *match_data;
    
    
    char * RegexStr = "(?:\\D|^)(5[1-5][0-9]{2}(?:\\ |\\-|)[0-9]{4}(?:\\ |\\-|)[0-9]{4}(?:\\ |\\-|)[0-9]{4})(?:\\D|$)";
    char * source = "5111 2222 3333 4444";
    
    pattern = (PCRE2_SPTR)RegexStr;// <<<<< This is where you pass your REGEX 
    subject = (PCRE2_SPTR)source;// <<<<< This is where you pass your bufer that will be checked. 
    subject_length = strlen((char *)subject);
    
    
    
    
      re = pcre2_compile(
      pattern,               /* the pattern */
      PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
      0,                     /* default options */
      &errornumber,          /* for error number */
      &erroroffset,          /* for error offset */
      NULL);                 /* use default compile context */
    
    /* Compilation failed: print the error message and exit. */
    if (re == NULL)
      {
      PCRE2_UCHAR buffer[256];
      pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
      printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,buffer);
      return 1;
      }
    
    
    match_data = pcre2_match_data_create_from_pattern(re, NULL);
    
    rc = pcre2_match(
      re,
      subject,              /* the subject string */
      subject_length,       /* the length of the subject */
      0,                    /* start at offset 0 in the subject */
      0,                    /* default options */
      match_data,           /* block for storing the result */
      NULL);
    
    if (rc < 0)
      {
      switch(rc)
        {
        case PCRE2_ERROR_NOMATCH: //printf("No match\n"); //
        pcre2_match_data_free(match_data);
        pcre2_code_free(re);
        Found = 0;
        return Found;
        //  break;
        /*
        Handle other special cases if you like
        */
        default: printf("Matching error %d\n", rc); //break;
        }
      pcre2_match_data_free(match_data);   /* Release memory used for the match */
      pcre2_code_free(re);
      Found = 0;                /* data and the compiled pattern. */
      return Found;
      }
    
    
    if (Debug){
    ovector = pcre2_get_ovector_pointer(match_data);
    printf("Match succeeded at offset %d\n", (int)ovector[0]);
    
    if (rc == 0)
      printf("ovector was not big enough for all the captured substrings\n");
    
    
    if (ovector[0] > ovector[1])
      {
      printf("\\K was used in an assertion to set the match start after its end.\n"
        "From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]),
          (char *)(subject + ovector[1]));
      printf("Run abandoned\n");
      pcre2_match_data_free(match_data);
      pcre2_code_free(re);
      return 0;
    }
    
    for (i = 0; i < rc; i++)
      {
      PCRE2_SPTR substring_start = subject + ovector[2*i];
      size_t substring_length = ovector[2*i+1] - ovector[2*i];
      printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
      }
    }
    
    else{
      if(rc > 0){
        Found = true;
    
        } 
    } 
    pcre2_match_data_free(match_data);
    pcre2_code_free(re);
    return Found;
    
    }
    

    Install PCRE using:

    wget https://ftp.pcre.org/pub/pcre/pcre2-10.31.zip
    make 
    sudo make install 
    sudo ldconfig
    

    Compile using :

    gcc foo.c -lpcre2-8 -o foo
    

    Check my answer for more details.

提交回复
热议问题