Regular Expressions in C Language

Source: Internet
Author: User
Tags preg

Regular Expressions, also known as regex or regexp, is a simple and flexible text processing tool. It can be used to precisely find the content that matches a specified rule in a text. For more information about regular expressions, see here. Common Regular Expression Libraries in C/C ++ include GNU Regex Library, Boost. Regex, PCRE, and PCRE ++. Among the four databases, the latter two are related, and the others are independent of each other, which are different implementations. Today, we mainly use the GNU Regex Library. Several major functions (1) regcomp: [cpp] int regcomp (regex_t * preg, const char * pattern, int cflags): Compile the regular expression pattern to be matched, preparation parameters before matching: preg, output parameters, used to save the compiled regular expression result pattern, input parameters, input the string cflags of the regular expression to be compiled, input parameters, it is used to specify the return values of some options in the regular expression matching process: Return 0 after compilation is successful, and return non-0 error codes int regcomp (regex_t * preg, const char * pattern, int cflags: compile the regular expression pattern to be matched, and prepare the parameters before matching: preg, output parameter, used to save the compiled regular expression result pattern, input parameter, input the cfl string of the regular expression to be compiled Ags, input parameter, used to specify the return values of some options in the regular expression matching process: 0 is returned for successful compilation, and non-0 error code is returned for failure (2) regexec: [cpp] int regexec (const regex_t * preg, const char * string, size_t nmatch, regmatch_t pmatch [], int eflags) function: used to detect whether the string matches the regular expression preg parameter: preg, input parameter, regular expression rule string compiled in (1) regcomp, input parameter, used to match the string nmatch, input parameter, this parameter is used to specify the pmatch length of the array corresponding to the pmatch parameter, and the output parameter is used to output the specific position eflag that matches the preg in the string. The input parameter, this parameter is used to specify the return values of some options in the regular expression matching process. If string matches the rule specified by preg, 0 is returned. Otherwise Returns non-0 int regexec (const regex_t * preg, const char * string, size_t nmatch, regmatch_t pmatch [], int eflags). function: used to detect whether the string matches the regular expression preg parameter: preg, input parameter, regular expression rule string compiled in (1) regcomp, input parameter, used to match the string nmatch, input parameter, this parameter is used to specify the pmatch length of the array corresponding to the pmatch parameter, and the output parameter is used to output the specific position eflag that matches the preg in the string. The input parameter, it is used to specify the return values of some options in the regular expression matching process: If string matches the rule specified by preg, 0 is returned; otherwise, non-0 (3) regerror: [plain] view plaincopyprint? Size_t regerror (int errcode, const regex_t * preg, char * errbuf, size_t errbuf_size) function: used to convert the error codes generated in regcompt and regexec into string format error information parameters: errcode, input parameter, error code returned in regcomp or regexec call preg, input parameter, the compiled regular expression structure errbuf corresponding to the error code, output parameter, buffer Used to return the error message. If the buffer size is not enough, the error message will be truncated by errbuf_size. input parameters, return the buffer size of the error message. Return Value: If errbuf_size is 0, then regerror returns the buffer size required by the error message size_t regerror (int errcode, const regex_t * preg, char * err Buf, size_t errbuf_size) function: used to convert the error codes generated in regcompt and regexec into string format error information parameters: errcode, input parameter, the error code preg returned in regcomp or regexec calls. Enter the parameters. The compiled regular expression structure errbuf corresponds to the error code. The output parameters are used to return the buffer of the error message, if the buffer size is not enough, the error message will be truncated by errbuf_size. Enter the parameter and return the buffer size of the error message. Return Value: If errbuf_size is 0, the buffer size required by the error message returned by regerror (4) regfree: [plain] view plaincopyprint? Void regfree (regex_t * preg) function: Used to release the memory parameter used by the preg structure generated during regcomp Compilation: preg, input parameter, structure pointer return value of the regular expression generated during regcomp Compilation: no void regfree (regex_t * preg) function: Used to release the memory parameters occupied by the preg structure generated during regcomp Compilation: preg, input parameter, structure pointer returned value of the regular expression generated during regcomp Compilation: no instance-Email match [plain] # include <stdio. h> # include <sys/types. h> # include <regex. h> int main (int argc, char ** argv) {int status, I; int cflags = REG_EXTENDED; regmatch_t pmatch [1]; const size_t nmatch = 1; regex_t reg; const char * pattern = "^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*. \ w + ([-.] \ w +) * "; char buf [256]; regcomp (®, Pattern, cflags); while (true) {printf ("input your email address: \ n"); gets (buf); if (status = regexec (®, Buf, nmatch, pmatch, 0) = 0) {printf ("Correct \ n"); // print match part for (I = pmatch [0]. rm_so; I <pmatch [0]. rm_eo; ++ I) putchar (buf [I]); printf ("\ n"); regfree (®); Break;} else {printf ("Error address, input again: \ n") ;}} return 0 ;}# include <stdio. h> # include <sys/types. h> # include <regex. h> int main (int argc, char ** argv) {int status, I; int cflags = REG_EXTENDED; regmatch_t pmatch [1]; const size_t nmatch = 1; regex_t reg; const char * pattern = "^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*. \ w + ([-.] \ w +) * "; char buf [256]; regcomp (®, Pattern, cflags); while (true) {printf ("input your email address: \ n"); gets (buf); if (status = regexec (®, Buf, nmatch, pmatch, 0) = 0) {printf ("Correct \ n"); // print match partfor (I = pmatch [0]. rm_so; I <pmatch [0]. rm_eo; ++ I) putchar (buf [I]); printf ("\ n"); regfree (®); Break;} else {printf ("Error address, input again: \ n") ;}} return 0 ;}

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.