Linux Learning-shell Verifying mailbox legitimacy with regular expressions

Source: Internet
Author: User
Tags alphabetic character alphanumeric characters subdomain subdomain name

Citation: in today's era, email has become an important means of communication. e-mails to mentors, communication between academic studies, etc. Email is often used, but the verification of email is a little difficult, and here is an example of using Shell scripting under Linux.

Linux combines regular expressions with sed, gawk, and is a powerful feature. I said is true, did not deceive you, studies well.

Mailbox Verification
The basic format of the email address is:

username@hostname
1.username Partial pattern matching

The value of username can be in characters with a number of substrings and the following special characters:

    • Point number
    • Single Dash
    • Plus
    • Underline

In a valid email user name, these characters can appear in any combination.

The @ sign is preceded by the hostname section

^([a-zA-Z0-9_\-\.\+]+)

' ^ ': Indicates that the entire pattern starts with the characters in front of it
' + ': Indicates that the preceding character appears at least once
' [] ': Indicates that any combination of symbols in brackets is OK

2.hostname pattern Matching

The hostname part of the email address consists of a domain name and a server name. The server name and domain name are also subject to strict naming conventions, and only alphanumeric characters and the following special characters are allowed:

    • Point number
    • Underline

The server name and domain name are separated by a dot, specify the server name, followed by the subdomain name, and finally the top-level domain with no dot number behind it.

Hostname mode is the part after @

([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$

' $ ': Indicates the end of the character before it
([a-zA-Z0-9_\-\.]+)Can be matched by a
Server
Server.subdomain
Server.subdomain.subsomain

For top-level domains, there are some special rules. A top-level domain name can be only an alphabetic character and must be no less than two characters long and not exceed 5 characters in length. The following is the regular expression pattern used for the top-level domain:

\.([a-zA-Z]{2,5})$

Once together, the entire pattern is as follows:

^([a-zA-Z0-9_\-\.\+]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$
3. Writing Shell script files

Write the above code in the script, isemail.sh.

#!/bin/bash  # This file use to check email  echo   "Please input email:"  read  emailstr= ' echo   $email  | Gawk /^ ([a-za-z0-9_\-\.\+]+) @ ([a-za-z0-9_\-\.] +)\. ([a-za-z]{2,5}) $/{print $} '  ' if  [!-n  ' ${str}  "]then   echo   "validation is wrong."         else  echo   "Your Eamil is: ${str} "  fi   

Description: The above code by reading the user entered the mailbox, to determine whether the mailbox entered is legitimate. Finally, output two different prompts.

4. Testing

Reference

[1]linux command line and Shell script programming Daquan Second Edition

The topic of Linux Learning Notes is continuously updated ...

Linux Learning-shell Verifying mailbox legitimacy with regular expressions

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.