Detailed explanation of using regular expressions 2_PHP tutorial

WBOY
Release: 2016-07-13 17:25:24
Original
969 people have browsed it

Detailed explanation of the use of regular expressions (2)
Usage examples
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.
Usually, the usage 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 replacement respectively. Operation.
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 =
 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:
   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 JavaScript JavaScript. 1.2 has a powerful RegExp() object that 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. >We can use JavaScript to write the following script to verify the validity of the email address entered by the user. 🎜> 




http://www.bkjia.com/PHPjc/532051.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/532051.html
TechArticle

Detailed explanation of the use of regular expressions (2) Usage examples After having a more comprehensive understanding of regular expressions, we Let’s take a look at how to use regular tables in Perl, PHP, and JavaScript...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!