After a more comprehensive understanding of regular expressions, let's look at how to use regular expressions in perl,php, and javascript/"target=" _blank ">javascript.
In general, Perl uses the following format for the expression:
Operator/regular-expression/string-to-replace/modifiers
An operator can be either m or S, representing the matching and substitution operations, respectively.
Where a regular expression is a pattern that will be matched or replaced, which can consist of any character, meta-character, or locator. Replace string an item is a string that is substituted for the found pattern match object when the S operator is used. The last parameter item is used to control different ways of matching or replacing. For example:
s/geed/good/
The first occurrence of the geed string will be found in the target object and replaced with a good. If we want to perform multiple find-and-replace 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 do not need to limit the matching case, the parameter "I" can be used. For example
m/jewel/i
The above regular expression will match the Jewel,jewel, or Jewel, in the target object.
In Perl, you use the specialized operator "=~" to specify a matching object for a regular expression. For example:
$flag =~ s/abc/abc/
The above regular expression will replace the string ABC in the variable $flag with ABC.
Below, 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 the user prefers PHP, the ereg () function can be used for pattern matching operations. The Ereg () function is used in the following format:
Ereg (pattern, string)
Where pattern represents the pattern of a regular expression, and string is the target object that performs a find-and-replace operation. The same is the verification email address, the program 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, we are looking at javascript/"target=" _blank ">javascript. The javascript/"target=" _blank ">javascript 1.2 has a powerful regexp () object that can be used to perform matching operations on regular expressions. The test () method can verify that the target object contains a matching pattern and returns TRUE or false accordingly.
We can use the javascript/"target=" _blank ">javascript to write the following script to verify the validity of the e-mail 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;
http://www.bkjia.com/PHPjc/532605.html www.bkjia.com true http://www.bkjia.com/PHPjc/532605.html techarticle after a fuller understanding of regular expressions, let's look at how to use regular expressions in perl,php, and javascript/"target=" _blank "javascript. Usually, ...