Regular expression usage (II) _ PHP Tutorial

Source: Internet
Author: User
Detailed explanation of the use of regular expressions (2 ). After a comprehensive understanding of regular expressions, let's take a look at how to use regular expressions in Perl, PHP, and JavaScripttarget_blankJavaScript. Generally, after a comprehensive understanding of regular expressions, let's take a look at how to use Perl, PHP, and use regular expressions in JavaScript/"target =" _ blank "> JavaScript.

Generally, regular expressions in Perl are in the following format:

Operator/regular-expression/string-to-replace/modifiers

An operator can be m or s, representing the matching operation and the replacement operation respectively.

A regular expression is a pattern for matching or replacement operations. it can contain any character, metacharacter, or positioning character. When the s operator is used to replace a string with the searched pattern matching object. The final parameter item is used to control different matching or replacement methods. For example:

S/geed/good/

Find the first geed string in the target object and replace it with good. If you want to perform multiple search-replace operations within the global scope of the target object, you can use the parameter "g", that is, s/love/lust/g.

In addition, if we do not need to restrict the matching case format, we can use the parameter "I ". For example,

M/JewEL/I

The above regular expression will match jewel, Jewel, or JEWEL in the target object.

In Perl, the special operator "= ~" is used ~" Specifies the matching object of the regular expression. For example:

$ Flag = ~ S/abc/ABC/

The above regular expression replaces the string abc in the variable $ flag with ABC.

Next, we will add a regular expression to the Perl program to verify the validity of the user's email address format. The code is as follows:

#! /Usr/bin/perl
# Get input
Print "Whats your email address ?";
$ Email =$ # @ 60; STDIN $ # @ 62 ;;
Chomp ($ email );
# Match and display result
If ($ email = ~ /^ ([A-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) + /)
{
Print ("Your email address is correct !");
}
Else
{
Print ("Please try again !");
}

If you prefer PHP, you can use the ereg () function for pattern matching. 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. Verify the email address. the code written in PHP is as follows:

$ #@ 60 ;? Php
If (ereg ('^ ([a-zA-Z0-9 _ + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) + ", $ email ))
{Echo "Your email address is correct !";}
Else
{Echo "Please try again !";}
? $ #@ 62;
Finally, let's take a look at JavaScript/"target =" _ blank "> JavaScript. JavaScript/"target =" _ blank "> JavaScript 1.2 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.

We can use JavaScript/"target =" _ blank "> JavaScript to write the following script to verify the validity of the email address entered by the user.

$ # @ 60; html $ # @ 62;
$ # @ 60; head $ # @ 62;
$ #@ 60; script language = "Javascript1.2" $ # @ 62;
$ # @ 60 ;! -- Start hiding
Function verifyAddress (obj)
{
Var email = obj. email. value;
Var pattern =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) + /;
Flag = pattern. test (email );
If (flag)
{
Alert ("Your email address is correct !");
Return true;
}
Else
{
Alert ("Please try again !");
Return false;
}
}
// Stop hiding -- $ # @ 62;
$ # @ 60;/script $ # @ 62;
$ # @ 60;/head $ # @ 62;
$ # @ 60; body $ # @ 62;
$ # @ 60; form onSubmit = "return verifyAddress (this);" $ # @ 62;
$ # @ 60; input name = "email" type = "text" $ # @ 62;
$ #@ 60; input type = "submit" $ # @ 62;
$ # @ 60;/form $ # @ 62;
$ # @ 60;/body $ # @ 62;
$ # @ 60;/html $ # @ 62;


Regular expressions are used in objective "target =" _ blank "JavaScript. Usually ,...

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.