How to use regular expression in C/C++ ?

how to code with regexp.h
some one can give me instance?
thx

regular expressions are not part of Standard C, you might find some
libraries for it. All i can suggest is searching for PCRE might give you something .

1 Like

regexp.h is part of UNIX. :slight_smile: So not a bad question. The manual page is a good start, see man regcomp. Also here is a short example.

#include <stdio.h>
#include <regex.h>
int main(void)
{
        regex_t reg;
        const char *regex="[abc]";
        const char *str="sadf";
        regmatch_t matches[16];

        regcomp(&reg, regex, REG_EXTENDED);

        if(regexec(&reg, str, 16, matches, 0) == 0)
        {
                printf("regex /%s/ matched string '%s' at bytes %d-%d\n",
                        regex, str, matches[0].rm_so, matches[0].rm_eo);
        }
        else
                printf("regex /%s/ does not match string '%s'\n", regex, str);
}
1 Like

I have a newbie.

I registered for the purpose of learn c++ language...

For C programs in Unix environments, the regex and the PCRE (Perl Compatible Regular Expressions) APIs, as suggested, will be the natural solution. The former is a POSIX.1 standard, and the later is also widely used, and specially in the open source world. PCRE also has a C++ wrapper that is provided by Google. For C++, the Boost libraries ( Boost C++ Libraries ) has support for regexes and are part of a proposal to the C++11 standard. That said, you have 3 good options. I would go first with the standard Unix regex API, because it's the "de facto" standard and its basic usage is very simple.