The use of regular expressions in network programming
[Foreword:] When we write a web program, it is often judged that a string validity, such as; whether a string is a number, whether it is a valid Email address, etc. If you do not use a regular expression, the program that judges will be very long, and it is easy to make mistakes. If you use the regular expression, these judgments are a very easy task. This article fully introduces the regular expressions, format. And add readers' sensibility understanding in PHP, ASP. Regular expressions are widely used, and you need to summarize in learning and practice. Regular expression brief introduction, the regular expression is a powerful tool that can be used for pattern matching and replacement. Applications in network programming, such as PHP scripting languages or JavaScript, client scripts such as VBScript provide 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 to verify that the format of the mail address input by the user online is correct. If the format of the user's mail address is verified by the regular expression, the form information filled in the user will be Normally; Conversely, if the user entered by the mail address does not match the pattern of regular expression, the prompt information will be popped up, requiring the user to re-enter the correct email address. This shows that the regular expression has a pivotable role in the logical judgment of the web application. Behind we will give an example detail. Regular expressions are generally as: / love /, where the "/" part of the "/" The segment is the mode to 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 {} ", or" / s, / s, / d, / w, and / w ", etc. In order to facilitate user more flexible setting matching mode Regular expression allows the user to use [] to define a character in a matching mode without being limited to a specific character. In addition to our metammatism, the regular expression also has another unique Special characters, ie, locator. Locator is used to specify the appearance position of the matching mode in the target object. More commonly used locators include: "^", "$", "/ b", "/ b". If we It is desirable to implement "or" or "operations in the regular expression. If you choose to match in multiple different modes, you can use the duct" | ". For example: there is a general purpose in the regular expression. The operator, ie, negative "[^].". Different the positioning character "^" mentioned above, the negative "[^]" specifies the string specified in the mode in the target object. Generally come Say, when "^" appears in "[]", it is considered a negative operator; and when "^" is "[]", or if "[]", it should be regarded as a locator. .
Finally, when the user needs to add a metamorphic in the regular expression of the regular expression and find the matching object, you can use the escape character "/". For example: / TH / * /, the regular expression will match "TH *" instead of "THE" in the target object. Regular expression of grammar rules and tags now we officially entered the expression of expressions, I will explain the usage of the expression according to the instance, after reading, you will feel that you will write UBB code so simple, as long as you follow step by step I learned to read this article, you become a UBB master. Exciting is that you can write your own UBB tags, no longer have to go to someone else to copy the code and template. Fortunately, Vbscritp5.0 provides us with the "Regular Expression" object, as long as your server is installed IE5.x, you can run. Character description: ^ Symbol match the beginning of the string. For example: ^ ABC matches "ABC XYZ" without matching the "XYZ ABC" matches the end of the string. For example: ABC $ matches "XYZ ABC" without matching "ABC XYZ". Note: If you use the ^ symbols and $ symbols at the same time, it will be accurately matched. For example, ^ ABC $ is only matched with "ABC" match * symbol matchs 0 or more front characters. For example, AB * can match "AB", "ABB", "ABBB", etc. symbols match at least one front character. For example: AB can match "ABB", "ABBB", etc., but do not match "ab". ? The symbol matches 0 or 1 front characters. For example: AB? C? Can only match "ABC", "ABBC", "ABCC", and "ABBCC". The symbol matches any character other than the commutation. For example: (.) Match all strings X | Y in addition to the wrapper match "x" or "y". For example: ABC | XYZ can match "ABC" or "XYZ", and "AB (C | X) YZ" matches "abcyz" and "abxyz" {n} matching characters in front of N times (n is non-negative) . For example: A {2} can match "aa", but do not match "a" {n,} matches the character in front of at least N (n is non-negative integer). For example: a {3,} matches "AAA", "AAAA", etc., but does not match "a" and "aa". Note: A {1,} equivalent to A a {0,} equivalent to a * {m, n} matches at least M, up to N-front characters. For example: A {1,3} matches "A", "AA" and "AAA". Note: A {0,1} is equivalent to a? [Xyz] represents a character set, which matches one of the characters in parentheses. For example: [ABC] Matches "A", "B" and "C" [^ xyz] represents a negative character set. Match any character in this parentheses.
For example, [^ ABC] can match any character [A-Z] other than "A", "B" and "C" represents a range of characters in a certain range, and match any characters within the specified interval. For example: [A-Z] matches any lower case character [^ m-n] from "A" to "Z" to represent characters outside a range, matching the characters within the specified range. For example: [m-n] matches any character / symbol from "M" to "N" is an escape operator. For example: / N wrap / F pacharge / R Enter // Matter // Match "/" // Match "/" / s any white character, including spaces, tabs, Page break, etc. Equivalent to "[/ f / n / r / t / v]" / s any non-blank characters. Equivalent to "^ / f / n / r / t / v]" / w word characters, including letters and underscores. Equivalent to "[A-ZA-Z0-9_]" / w any non-word characters. Equivalent to "[^ A-ZA-Z0-9_]" / B match the end of the word. For example: VE / B matches the word "love", but does not match "Very", "Even" and other / b match the beginning of the word. For example: VE / B match words "Very", etc., but do not match "love" / D matching a numeric character, equivalent to [0-9]. For example: ABC / DXYZ matches "ABC2xyz", "ABC4xyz", etc., but does not match "abcaxyz", "ABC-XYZ", etc. / D matching a non-digital character, equivalent to [^ 0-9]. For example: ABC / DXYZ matches "abcaxyz", "ABC-XYZ", etc., but does not match "abc2xyz", "ABC4xyz", "ABC4xyz", etc. / NUM match NUM (where Num is a positive integer), reference to the match to remember. For example: (.) / 1 Match two consecutive identical characters. / ONUM matches N (where N is an octave extension value of one less than 256). For example: / O011 matching tab / XNUM matches NUM (where Num is a hexadecimal code value of less than 256). For example: / x41 Matching Character "A" application instance After the regular expression has a more comprehensive understanding, you can use the regular expression in Perl, PHP, and ASP. The following is a PHP language as an example, using the authenticated user online input, and whether the format of the URL is correct. PHP provides an EREGI () or EREG () data processing function implementation string compared to profiling mode matching operation EREG () function's usage format is as follows: EREG (Pattern, String) Where Pattern represents the regular expression; and String Then the target object that looks for the replacement operation, such as the email address value. This formats analyzes the bit string String with Pattern rules and finds that the return value is TRUE. The difference between the letter EREG () and EREGI () is that the former is case sensitive, the latter is not related to the case.
The program code written using PHP is as follows: Php if (EREG ("^ ([A-Z0-9 _-]) @ ([A-ZZ0-9_-]) (/. [A-Z0-9_- ]) [AZ] {2, 3} $ ", $ email) {echo" Your E-mail is not a legitimate E-mail address, please re-enter! " ;}?> This example is a simple check that can be entered to the user, check if the user's E-mail string is @ 字, in the @ 字 元 英文 英文 英文, digital or lower " _ ", There are several sketches after @, only two or three lowercase English letters after the last decimal point. Such as webmaster@mail.sever.net, hello_2001@88new.cn can pass the check, and new99@253.com (uppercase letters) and new99@253.comn (only more than 3 English letters after the last decimal point) Can't pass the inspection. We can also check the function by calling custom regular regulations, such as the following URL inspection Function: Function VerifyWebsiteaddr ($ StrWebsiteaddr) {Return (EREGI ("^ ([[_ 0-9a-z -] .) . ([0-9A-Z -] .) [AZ] {2, 3} $ ", $ StrWebsiteAddr);} We know that the PHP program must have server support if you want to think on your homepage Realize the above functions, embedded scripting language JavaScript may be a good choice. JavaScript has a powerful regexp () object, which 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. A JavaScript code is only required to add a JavaScript code in the
area of the HTML document.