Teach you how to use regular expressions in C language skillfully

Source: Internet
Author: User
Tags preg regular expression linux

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.