After having a more comprehensive understanding of regular expressions, let’s take a look at how to use regular expressions in Perl, PHP, and JavaScript/" target="_blank">JavaScript.
Usually, the format of regular expressions in Perl is as follows:
Operator / regular-expression / string-to-replace / modifiers
The operator item can be m or s, which represent matching operation and substitution operation respectively.
Among them, the regular expression item is the pattern to be matched or replaced, which can be composed of any characters, metacharacters, or locators. The replacement string item is the string that replaces the found pattern matching object when using the s operator. The last parameter is used to control different matching or replacement methods. For example:
s/geed/good/
Will find the first occurrence of the geed string in the target object and replace it with good. If we want to perform multiple search-replace operations in the global scope of the target object, we can use the parameter "g", that is, s/love/lust/g.
In addition, if we do not need to limit the matching case, 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, use the special operator "=~" to specify the matching object of the regular expression. For example:
$flag =~ s/abc/ABC/
The above regular expression will replace the string abc in the variable $flag with ABC.
Next, we will add regular expressions 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 usage format of the ereg() function is as follows:
ereg(pattern, string)
Among them, pattern represents the pattern of the regular expression, and string is the target object for performing the search and replace operation. The same is to verify the 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, let’s take a look at JavaScript/" target="_blank">JavaScript. JavaScript/" target="_blank">JavaScript 1.2 comes with a powerful RegExp() object. It can be used to perform regular expression matching operations. The test() method can check whether the target object contains a matching pattern and return true or false accordingly.
$#@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; 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;