The application of regular expression in Network programming (3)
Source: Internet
Author: User
Application examples
With a more comprehensive understanding of regular expressions, you can use regular expressions in perl,php, as well as in programs such as ASP.
The following is an example of the PHP language, using the verify that the e-mail address entered by the user online and the URL is in the correct format. PHP provides a eregi () or ereg () data processing function to implement string alignment profiling of pattern matching operations the use format of the Ereg () function is as follows:
Ereg (pattern, string)
Where pattern represents the schema of the regular expression, and string is the target object, such as an email address value, that performs a find-and-replace operation. This function is used to parse string strings based on pattern rules, and the return value is true if found. The difference between a function ereg () and a eregi () is that the former is case-sensitive and the latter has nothing to do with case. The program code written using PHP is as follows:
<?php
if (Ereg ("^ [a-z0-9_-]) +@ ([a-zz0-9_-]) + (. [ a-z0-9_-]) +[a-z]{2,3}$ ", $email))
{echo "Your e-mail through the preliminary check!" ";}
Else
{echo "is not a valid e-mail address, please re-enter!") ";}
? >
This example is a simple check for user-entered e-mail. Check whether the user's e-mail string has @ characters, in the @ character before the lowercase English letters, numbers or "_", after the @ There are several sections of string, the last decimal point can only have two or three lowercase English letters. such as Webmaster@mail.sever.net, hello_2001@88new.cn can be checked, while New99@253.com (uppercase letters) and New99@253.comn (the last decimal point can only be more than 3 English letters) Can not pass the examination.
We can also check operations by invoking custom positive rule discriminant functions, such as the following URL validation function:
function Verifywebsiteaddr ($strWebSiteAddr) {
Return (eregi ("^" [_0-9a-z-]+.) + ([0-9a-z-]+.) +[a-z]{2,3}$ ", $strWebSiteAddr));
}
We know that PHP programs must run with server support, and embedded scripting language JavaScript may be a good choice if you want to implement the above features on your home page. JavaScript comes with a powerful regexp () object that can be used to match regular expressions. The test () method can verify that the target object contains a match pattern and returns TRUE or false accordingly. You only need to add a section of JavaScript code to 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_-]) +/;
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