Regular expression use detailed (2) Posted Time: 2003-5-9 18:21:40 Author: Flying Eagle Click: 1759 Source: www.aspcool.com
Regulations on regular expressions (2)
Use example
After the regular expression has a more comprehensive understanding, let's take a look at how to use regular expressions in Perl, PHP, and JavaSRCIPT.
Typically, the usage format of the regular expression in Perl is as follows:
Operator / regular-expression / string-to-replace / modifiers
One of the operators can be M or S, represent matching operations and replacement operations, respectively.
Among them, the regular expression is a mode that will match or replace the operation, can be composed of any character, element character, or locator. The replacement string is a string that matches the object to the object when the search mode matchs the object. The final parameter item is used to control different match or replacement. E.g:
S / geed / good /
The first GEED string will be found in the target object and replace it with a good. If we want to perform multiple lookups-replacement operations in the global scope of the target object, you can use the parameter "g", which is S / Love / Lust / G.
In addition, if we don't need to limit the case where you don't need to limit the matching form, you can use the parameter "I". E.g,
m / jewel / i
The above regular expression will match Jewel, Jewel, or Jewel in the target object.
In Perl, use specialized operators "= ~" to specify matching objects of regular expressions. E.g:
$ FLAG = ~ S / ABC / ABC /
The above regular expression will replace the string ABC in the $ FLAG to ABC.
Below, we add regular expressions in the Perl program to verify the validity of the user's mail address format. code show as below:
#! / usr / bin / perl
# get input
Print "What's your email address? / n";
$ EMAIL =
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! / n");
}
Else
{
Print ("please try! / n");
}
If the user prefer PHP, you can use an EREG () function to match the mode matching operation. The use format of the EREG () function is as follows:
EREG (Pattern, String)
Among them, Pattern represents the mode of the regular expression, and String is the target object that performs the lookup replacement operation. Similarly, verifying the email address, the program code written in PHP is as follows:
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!";
?>
Finally, let's take a look at JavaSRCIPT. JavaSRCIPT 1.2 has a powerful regexp () object that can be used to match the regular expression. The test () method can verify that there is a match mode in the target object and return TRUE or FALSE accordingly. We can write the following scripts using JavaSrcipt to verify the validity of the mail address entered by the user.
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!");
Return False;
}
}
// stop hiding ->
srcipt>
hEAD>