How to use regular expressions in C?

27,714

Solution 1

You can use PCRE:

The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own native API, as well as a set of wrapper functions that correspond to the POSIX regular expression API. The PCRE library is free, even for building commercial software.

See pcredemo.c for a PCRE example.

If you cannot use PCRE, POSIX regular expression support is probably available on your system (as @tinkertim pointed out). For Windows, you can use the gnuwin Regex for Windows package.

The regcomp documentation includes the following example:

#include <regex.h>

/*
 * Match string against the extended regular expression in
 * pattern, treating errors as no match.
 *
 * Return 1 for match, 0 for no match.
 */

int
match(const char *string, char *pattern)
{
    int    status;
    regex_t    re;

    if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
        return(0);      /* Report error. */
    }
    status = regexec(&re, string, (size_t) 0, NULL, 0);
    regfree(&re);
    if (status != 0) {
        return(0);      /* Report error. */
    }
    return(1);
}

Solution 2

If forced into POSIX only (no pcre), here's a tidbit of fall back:

#include <regex.h>
#include <stdbool.h>

bool reg_matches(const char *str, const char *pattern)
{
    regex_t re;
    int ret;

    if (regcomp(&re, pattern, REG_EXTENDED) != 0)
        return false;

    ret = regexec(&re, str, (size_t) 0, NULL, 0);
    regfree(&re);

    if (ret == 0)
        return true;

    return false;
}

You might call it like this:

int main(void)
{
   static const char *pattern = "/foo/[0-9]+$";

   /* Going to return 1 always, since pattern wants the last part of the
    * path to be an unsigned integer */
   if (! reg_matches("/foo/abc", pattern))
       return 1;

   return 0;
}

I highly recommend making use of PCRE if its available. But, its nice to check for it and have some sort of fall back.

I pulled the snippets from a project currently in my editor. Its just a very basic example, but gives you types and functions to look up should you need them. This answer more or less augments Sinan's answer.

Share:
27,714
jeffkolez
Author by

jeffkolez

I like to think outside the box. I bring solutions to problems from all areas of life. I love cooking; there’s some overlap between cooking and programming. The meal (and project) is won or lost in the preparation. I’m a minimalist at heart, and I love simplicity. There’s nothing more satisfying than making a commit that removes more lines of code than it adds. I do my best to make things better. I prioritize learning, and I love speaking at conferences. I attend as many programming conferences as I can fit into my schedule. I look for ways to solve inefficiencies. I want to eliminate inefficiencies within applications, code, teams, communication, as well as processes. I look at the big picture of where the company’s headed and make long-term plans for scalability and growth. I balance that long-term plan with short-term goals. I put my heart into everything I do. I live in Southern Ontario, just outside of Toronto with my partner, my two kids, and our dogs, and cat.

Updated on December 23, 2020

Comments

  • jeffkolez
    jeffkolez over 3 years

    I need to write a little program in C that parses a string. I wanted to use regular expressions since I've been using them for years, but I have no idea how to do that in C. I can't find any straight forward examples (i.e., "use this library", "this is the methodology").

    Can someone give me a simple example?