Application of Regular Expressions in network programming (3)

Source: Internet
Author: User
Tags ereg valid email address

Application Instance
After a comprehensive understanding of regular expressions, you can use regular expressions in programs such as Perl, PHP, and ASP.
The following uses the PHP language as an example to verify whether the email address entered by the user online and the URL format are correct. PHP provides the eregi () or ereg () data processing functions to implement the pattern matching operation for string comparison and analysis. The format of the ereg () function is as follows:
Ereg (pattern, string)
Here, pattern indicates the regular expression mode, while string indicates the target object for performing the search and replacement operation, such as the Email address value. This function analyzes the comparison string Based on the pattern Rule. If it is found, the return value is true. The difference between ereg () and eregi () is that the former is case sensitive, and the latter is case insensitive. The program code written in PHP is as follows:
<? Php
If (ereg ("^ ([a-z0-9 _-]) + @ ([a-zZ0-9 _-]) + (. [a-z0-9 _-]) + [a-z] {2, 3} $ ", $ email ))
{Echo "your email has passed the preliminary check! ";}
Else
{Echo "is not a valid email address. Please enter it again! ";}
?>

In this example, you can perform a simple check on the E-Mail entered by the user to check whether the user's E-Mail string contains the @ character, there are lower-case English letters, numbers, or lower-case "_" before the @ character. There are several strings after @, and only two or three lower-case English letters can be entered after the decimal point. For example, webmaster@mail.sever.net can pass the check, while hello_2001@88new.cn (with uppercase letters) and New99@253.com (with only three English letters after the last decimal point) cannot pass the check.
We can also perform the check operation by calling the custom positive rule discriminant function, as shown in the following URL verification function:
Function VerifyWebSiteAddr ($ strWebSiteAddr ){
Return (eregi ("^ ([_ 0-9a-z-] + .) + ([0-9a-z-] + .) + [a-z] {2, 3} $ ", $ strWebSiteAddr ));
}
We know that PHP programs must be supported by servers. If you want to implement the above functions on your home page, the embedded scripting language Javascript may be a good choice. JavaScript contains a powerful RegExp () object that can be used to match regular expressions. The test () method can check whether the target object contains the matching mode and return true or false accordingly. You only need to add a piece of Javascript code in the <Language = "Javascript1.2">
Function verifyAddress (obj ){
Var email = obj. email. value;
Var pattern =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) + /;

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.