Regulations on regular expressions (2)

zhaozj2021-02-11  187

This blog stops updating, the new home address http://ashun.cnblogs.com/

Regular Expression Use Detailed Detailed (2) After using an instance after a comprehensive understanding of the regular expression, let's take a look at how to use the regular expression in Perl, PHP, and JavaScript. Typically, the usage format of regular expressions in Perl is as follows: Operator / regular-expression / string-to-replace / modifiers operator one 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. For example: S / Geed / Good / will look for the first GEED string 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". For example, 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. For example: $ FLAG = ~ S / ABC / ABC / The above regular expression will replace the string ABC in the Variable $ FLAG to ABC. Below, we add regular expressions in the Perl program to verify the validity of the user's mail address format. The code is as follows: #! / Usr / bin / perl # GET INPUT Print "What's your email address" / n "; $ EMAIL = chomp ($ email); # match and display results ing ($ 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) where pattern represents the regular expression, and String is the target object that performs the lookup replacement operation. The same is to verify the email address, the program code written in PHP is as follows: Finally, we are Let's take a look at JavaScript. JavaScript 1.2 has a powerful regexp () object that can be used to perform a matching operation of regular expressions. The test () method can verify that there is a match mode in the target object and return TRUE or FALSE accordingly.

转载请注明原文地址:https://www.9cbs.com/read-4170.html

New Post(0)