C-language implementation of e-mail address verification Program _c language

Source: Internet
Author: User
Tags posix

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.

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.