If users are familiar with the SED, awk, grep, or VI under Linux, then the concept of regular expressions is certainly not unfamiliar. Because it can greatly simplify the complexity of processing strings, it is now being applied in many Linux utilities. Never assume that regular expressions are just a patent for scripting languages like Perl, Python, Bash, and as C programmers, users can also use regular expressions in their own programs.
Both standard C and C + + do not support regular expressions, but there are libraries that can assist C + + programmers to complete this function, most notably when Philip Hazel's perl-compatible Regular expression Library, Many Linux distributions have this library of functions.
Compiling regular expressions
To improve efficiency, before comparing a string to a regular expression, you first compile it with the Regcomp () function and convert it to a regex_t structure:
int regcomp(regex_t *preg, const char *regex,
int cflags);
The parameter regex is a string that represents the regular expression to be compiled; The argument preg to a data structure declared as regex_t to hold the compilation result; The argument cflags determines the details of how the regular expression should be handled.
If the function Regcomp () executes successfully and the compilation result is correctly populated into Preg, the function returns 0, and any other return result represents some sort of error generation.
Matching regular expressions
Once you have successfully compiled the regular expression with the Regcomp () function, you can then call the Regexec () function to complete the pattern match:
int regexec(const regex_t *preg,
const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags);
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
The parameter preg points to the compiled regular expression, and the parameter string is the string that will be matched, while the arguments Nmatch and Pmatch are used to return the matching result to the caller, and the last parameter eflags determines the details of the match.