Compile a regular expression
#include <regex.h> int regcomp( regex_t * preg, const char * pattern, int cflags );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
This function is in libc.a, but not in libc.so (in order to save space). |
The regcomp() function prepares the regular expression, preg, for use by the function regexec(), from the specification pattern and cflags. The member re_nsub of preg is set to the number of subexpressions in pattern.
The functions that deal with regular expressions (regcomp(), regerror(), regexec(), and regfree()) support two classes of regular expressions, the Basic and Extended Regular Expressions. These classes are rigorously defined in IEEE P1003.2, Regular Expression Notation.
The Basic Regular Expressions are composed of these terms:
The Extended Regular Expressions also include:
/* The following example prints out all lines from FILE "f" that match "pattern". */ #include <stdio.h> #include <regex.h> #include <limits.h> #define BUFFER_SIZE 512 void grep( char* pattern, FILE* f ) { int t; regex_t re; char buffer[BUFFER_SIZE]; if ((t=regcomp( &re, pattern, REG_NOSUB )) != 0) { regerror(t, &re, buffer, sizeof buffer); fprintf(stderr,"grep: %s (%s)\n",buffer,pattern); return; } while( fgets( buffer, BUFFER_SIZE, f ) != NULL ) { if( regexec( &re, buffer, 0, NULL, 0 ) == 0 ) { fputs( buffer, stdout ); } } regfree( &re ); }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
Henry Spencer. For license information, see the Third Party License Terms List at http://licensing.qnx.com/third-party-terms/.
regerror(), regexec(), regfree()