Regular expression

zhaozj2021-02-16  55

Regular expression

March 23,2004 If we ask those Unix systems, they like what they like, in addition to a stable system and can be started remotely, people will mention regular expressions; if we ask them the most What can be, in addition to complex process control and installation processes, will also be regular expressions. So what is the regular expression? How can I really master the regular expression and properly use it? This article will introduce this, hoping to help readers who are eager to understand and master regular expressions. Introduction to the entry is simple, the regular expression is a powerful tool that can be used for pattern matching and replacement. We can find a regular expression in almost all UNIX-based tools, such as a VI editor, Perl, or PHP scripting language, and awk or sed shell programs. In addition, the scripting language like JavaScript has also provided support for regular expressions. It can be seen that the regular expression has exceeded the limitations of some languages ​​or a system, and has become a widely accepted concept and function. Regular expression allows users to build a matching mode by using a series of special characters, then compare the matching mode with data files, program input, and web pages, whether or not to include matching mode in the comparison object, perform corresponding program of. For example, a general expression of a regular expression is whether it is used to verify that the format of the mail address entered online input is correct. If the format of the user mail address is verified by the regular expression, the form information filled out will be processed normally; contrary, if the user entered by the user input does not match the mode, the prompt information will be popped up, requiring the user to re-re- Enter the correct email address. This shows that the regular expression has a pivotable role in the logical judgment of the web application. After the basic syntax has a preliminary understanding of the functions and functions of the regular expression, we will see the syntax format of the regular expression. Regular expressions are generally as follows: / LOVE / where "/" The part is located between the "/" delimiter, is the mode that will match in the target object. Users can put them between the mode content you want to find the matching object in the "/" delimiter. In order to be able to make user more flexible custom mode content, regular expressions provide special "metadamic characters". The so-called metammatism refers to the exhibit mode of its preamble characters (i.e., characters in front of the metamorphism) in the regular expression. More commonly used metamodes include: " ", "*", and "?". Among them, " " figures specify that its predetermined characters must continue once or more in the target object, "*" element character specifies that its predetermined character must occur zero or continuous in the target object, and "?" Yuan Characters are specified that their leading objects must be zero or once in the target object. Let's take a look at the specific application of the regular expression element character. / fo / because of the above regular expression contains " " character, indicating "fool", "fo", or "football", etc., etc., or "football", etc., or "football", or "football", etc. String match. / EG * / Because of the above regular expression contains "*" metad character, it indicates that "Easy", "EGO", or "EGG", or "EGG", or the like can be continuously or more letter G after the letter E. The string matches. / Wil? / Since the above regular expression contains "?" Metadam, it indicates that "WIN", or "Wilson" in the target object, or the like of zero or one letter L continuously after the letter i. match. In addition to the metammat, the user can accurately specify the frequency that appears in the match object.

For example, / jim {2,6} / The regular expression specifies that the character m can continuously appear 2-6 times in the matching object, and therefore, the regular expression may match the character string such as JIMMY or JIMMMMMY. After you have a preliminary understanding of how to use the regular expression, let's take a look at the other important metades. s: Used to match a single space character, including a Tab key, and a newline break; S: Used to match all characters outside of single spaces; D: Used to match numbers from 0 to 9; W: Used to match letters, Digital or underscore characters; W: Used to match all characters with W mismatches;.: Used to match all characters outside of the restroom. (Description: We can see S and S and W and W as an inverse perimeter), we can use the example how to use the above metamic characters in regular expressions. / S / The above regular expression can be used to match one or more space characters in the target object. / D000 / If we have a complex financial statement in his hand, we can find all the total amount of thousands of yuan through the above-mentioned regular expressions. In addition to the metamorphors described above, there is another unique dedicated character, ie, locator in the regular expression. The locator is used to specify the appearance of the matching mode in the target object. More commonly used locators include: "^", "$", "b", and "b". Where "^" positioning specifies that the matching mode must appear at the beginning of the target string, the "$" locator specified that the matching mode must appear in the end of the target object, the B locator specified that the matching mode must appear at the beginning of the target string or One of the two boundaries of the end, and the "B" positioning runation specifies that the matching object must be within the beginning and end of the target string, that is, the matching object cannot be the beginning of the target string, and cannot be used as a target character. The end of the string. Similarly, we can also regard "^" and "$" and "b" and "b" as two sets of locators that are mutually counters. For example: / ^ Hell / Since the "^" locator is included in the above regular expression, you can match the string of "Hell", "Hello" or "Hellhound" in the target object. / Ar $ / because the "$" locator is included in the above regular expression, so it can match the string end of "car", "bar" or "ar" in the target object. / BBOM / Since the above regular expression mode begins with the "B" locator, it can match the string of "BOMB", or "BOM" in the target object. / MANB / Since the above regular expression mode is tailing with the "B" locator, it can match the string of "hum", "Woman" or "man" in the target object. In order to facilitate user more flexible setting matching mode, the regular expression allows the user to specify a range in the match mode without being limited to the specific character. For example: / [a-z] / The above regular expression will match any uppercase letter from the A to Z. / [A-z] / The above regular expression will match any lowercase from the A to Z. / [0-9] / The above regular expression will match any of the numbers from 0 to 9. / ([A-Z] [A-Z] [0-9]) / The regular expression will match any string composed of letters and numbers, such as "AB0". Here, it is necessary to remind the user to pay attention to the use of "()" to combine the string in the regular expression. "()" The content containing the symbol must appear in the target object at the same time. Therefore, the above regular expression will not match a string such as "ABC", because the last character in "ABC" is a letter rather than a number.

If we want to implement "or" or "operations in the regular expression, you can use a match in multiple different modes to use the pipeline" | ". For example: / to | TOO | 2 / The above regular expressions will match "to", "TOO", or "2" in the target object. There is also a more common operator in the regular expression, ie, negative "[^]". Unlike the locator "^" described in our forebel, negative "[^]" specifies the string specified in the mode in the target object. For example: / [^ a-c] / The above string will match any character other than A, B, and C in the target object. In general, when "^" appears in "[]", it is considered a negative operator; and when "^" is "[]", or "[]", it should be regarded. Locator. Finally, when the user needs to add a metammat in the regular expression of the regular expression, and find the escape character when it looks for match objects. For example: / TH * / The regular expression will match "TH *" instead of "THE" in the target object. Using an example After the regular expression has a more comprehensive understanding, 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.

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

New Post(0)