Regular expressions in C: examples?

后端 未结 5 1097
傲寒
傲寒 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:27

    man regex.h reports there is no manual entry for regex.h, but man 3 regex gives you a page explaining the POSIX functions for pattern matching.
    The same functions are described in The GNU C Library: Regular Expression Matching, which explains that the GNU C Library supports both the POSIX.2 interface and the interface the GNU C Library has had for many years.

    For example, for an hypothetical program that prints which of the strings passed as argument match the pattern passed as first argument, you could use code similar to the following one.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    void print_regerror (int errcode, size_t length, regex_t *compiled);
    
    int
    main (int argc, char *argv[])
    {
      regex_t regex;
      int result;
    
      if (argc < 3)
        {
          // The number of passed arguments is lower than the number of
          // expected arguments.
          fputs ("Missing command line arguments\n", stderr);
          return EXIT_FAILURE;
        }
    
      result = regcomp (®ex, argv[1], REG_EXTENDED);
      if (result)
        {
          // Any value different from 0 means it was not possible to 
          // compile the regular expression, either for memory problems
          // or problems with the regular expression syntax.
          if (result == REG_ESPACE)
            fprintf (stderr, "%s\n", strerror(ENOMEM));
          else
            fputs ("Syntax error in the regular expression passed as first argument\n", stderr);
          return EXIT_FAILURE;               
        }
      for (int i = 2; i < argc; i++)
        {
          result = regexec (®ex, argv[i], 0, NULL, 0);
          if (!result)
            {
              printf ("'%s' matches the regular expression\n", argv[i]);
            }
          else if (result == REG_NOMATCH)
            {
              printf ("'%s' doesn't the regular expression\n", argv[i]);
            }
          else
            {
              // The function returned an error; print the string 
              // describing it.
              // Get the size of the buffer required for the error message.
              size_t length = regerror (result, ®ex, NULL, 0);
              print_regerror (result, length, ®ex);       
              return EXIT_FAILURE;
            }
        }
    
      /* Free the memory allocated from regcomp(). */
      regfree (®ex);
      return EXIT_SUCCESS;
    }
    
    void
    print_regerror (int errcode, size_t length, regex_t *compiled)
    {
      char buffer[length];
      (void) regerror (errcode, compiled, buffer, length);
      fprintf(stderr, "Regex match failed: %s\n", buffer);
    }
    

    The last argument of regcomp() needs to be at least REG_EXTENDED, or the functions will use basic regular expressions, which means that (for example) you would need to use a\{3\} instead of a{3} used from extended regular expressions, which is probably what you expect to use.

    POSIX.2 has also another function for wildcard matching: fnmatch(). It doesn't allow to compile the regular expression, or get the substrings matching a sub-expression, but it is very specific for checking when a filename match a wildcard (e.g. it uses the FNM_PATHNAME flag).

提交回复
热议问题