The most common validation that we encounter is email address verification. Common on the site. Various web scripts are also commonly used in "regular expression" (regular expression) to verify the email address we entered, to determine whether it is legal. Some can also break out the user name and domain name. Now implement the e-mail address validator in C, with a POSIX regular expression . Because you are using POSIX regular expressions, you can only compile with GCC.
In addition, it is tested that only the Linux version of GCC can be compiled. If compiling with MinGW or Cygwin gcc in Windows does not pass, the error is that the <regex.h> header file cannot be found. The
C language source code is as follows:
* * REGEX.C-Validate e-mail addresses with regular expressions * * POSIX Regular expression * * * compile command: * GCC regex.c-o regex-wall * * * Copyright Ye Jianfei 20 * * */#include <stdio.h> #include <stdlib.h> #include <string.h> #include <regex.h>/* Regula
R expression Regular Expression */#define MAX_LENGTH 1000 int main (void) {size_t char_num;
Char Email_address[max_length];
Char User_name[max_length];
Char Domain_name[max_length];
regmatch_t Pmatch[4];
regex_t Match_regex; Regcomp (&match_regex, "[0-9a-za-z\\-_\\.] +) @ ([0-9a-z]+\\. [A-z] {2,3} (\\. [A-z]
{2})?) ", reg_extended);
while (scanf ("%s", email_address)!= EOF) {if (Regexec (&match_regex, email_address, 4, pmatch, 0) = 0) {
Char_num = Pmatch[1].rm_eo-pmatch[1].rm_so;
strncpy (user_name, & (Email_address[pmatch[1].rm_so]), char_num);
User_name[char_num] = ' the ';
Char_num = Pmatch[2].rm_eo-pmatch[2].rm_so;
strncpy (domain_name, & (Email_address[pmatch[2].rm_so]), char_num); Domain_name[char_nuM] = ' I ';
Puts ("The email address you entered is valid");
printf ("Username:%s\n", user_name);
printf ("Domain name:%s\n\n", domain_name);
Else puts ("The e-mail address you entered is not legal \ n \ nthe");
} regfree (&match_regex);
return exit_success;
}
Before the article introduced a number of methods for verifying regular expressions in the mailbox, JS Regular expression Verification email address, PHP e-mail address regular expression verification, this is the C language to achieve e-mail address verification, I hope that we combine relevant articles to learn from, get harvest.