Use regular expressions compatible with Perl in PHP

xiaoxiao2021-03-06  51

Source: PHP Power Online 1 Preface PHP PHP is developed in a large number of background CGI development, which is usually a result after the user data data, but if the user entered is incorrect, there will be problems, such as someone The birthday is "February 30"! How should I check if the summer vacation is correct? Support for regular expressions in PHP, allowing us to make data matching. 2 What is regular expression: Simple, regular expression is a powerful tool that can be used for pattern matching and replacement. Traces of regular expressions in almost all UNIX / Linux systems are found, for example: Perl or PHP scripting languages. In addition, JavaScript's scripting language also provides support for regular expressions, and now regular expressions have become a general concept and tool, which is widely used by all kinds of technicians. There is this in a Linux website: "If you ask Linux lovers favorite, he may answer the regular expression; if you ask him the most fear, he will definitely say that he will say that he will say that he will definitely "As mentioned above, the regular expression looks very complicated, which is scared, most PHP beginners will skip this, continue the following learning, but the regular expression in PHP has a match to find compliance with mode matching The condition of the string, determine whether the string is critical or use the specified string to replace the powerful functions such as the conformity string, and it is unfortunately ... 3 The basic sympathization of the regular expression: a regular expression, Divided into three parts: separator, expressions, and modifiers. The separator may be any character other than a special character (such as "/!", Etc.), the commonly used separator is "/". Expression consists of some special characters (special characters, see below) and non-special strings, such as "[A-Z0-9 _-] @ [A-Z0-9 _-.] " Can match a simple electronics Mail string. The modifier is used to turn on or off some function / mode. The following is an example of a complete regular expression: /Hello. ?hello/is's regular expression "/" is the separator, between the two "/" is expressions, the second "/" behind The string "IS" is a modifier. In the expression, if the separator is included, it is necessary to use the escape symbol "/", such as "/Hello. //Hello/is". In addition to the specific characters that can be performed outside the separator, all special characters composed of letters require "/" to escape, such as "/ d" represents all numbers. 4 Special characters for regular expressions: Special characters in regular expressions are divided into element characters, locating characters, and more. The metammathe is a kind of characteristic character in the regular expression, used to describe its preamble character (ie characters in front of the element) appear in the matched object. The metadature itself is a single character, but the different or the same metammatic combination can constitute a large element character. Metacity: Big Big Number: Braces Use to accurately specify the number of matches, such as "/ pre {1, 5} /" means that the matching object can be "pre", "pree", "preeee" " 1 to 5 "E" strings appear behind the PR. Or "/ pre {, 5} /" represents 0 this to 5 times between 0 this.

Plus: " " character is used to match characters before the character appear or multiple. For example, "/ ac /" means that the object being matched may be "ACT", "Account", "ACCCC", etc., "A", "A", "A" strings, such as "A", or more "C" strings. " " Is equivalent to "{1,}". The asterisk: "*" is used to match the characters before matching the character. For example, "/ ac * /" indicates that the matching object can be "app", "ACP", "ACCP", etc., the string of zero or more "C" appears after "A". "*" Is equivalent to "{0,}". Question mark: "?" The characters used to match the characters in front of the character appear zero or 1 time. For example, "/ ac? /" Indicates that the matching object can be "a", "ACP", "ACWP" such as zero or 1 "C" string after "A". "?" There is also a very important role in the regular expression, "greed mode". There are two very important characters that "[]". They can match the characters that appear in "[]", such as "/ [az] /" can match a single character "a" or "z"; if the above expression is changed to "/ [AZ] /" You can match any single lowercase letters, such as "a", "b", and more. If "^" appears in "[]", the characters that appear in this expression do not match "[]", such as "/ [^ a-z] /" do not match any lowercase letters! And the regular expression gives a few "[]" defaults: [: alpha:]: Match any letters [: alnum:]: Match any letters and numbers [: Digit:]: Match any number [: Space: ]: Matching Air Clearance [: Upper:]: Match any uppercase letters [: Lower:]: Match any lowercase letters [: Punct:]: Match any punctuation [: xdigit:]: Match any 16 credit number Special characters The meaning of the representative after the escape symbol "/" escape: S: Match a single space for all characters used to match the unique spaces. D: Used to match numbers from 0 to 9, equivalent to "/ [0-9] /". W: Used to match letters, numbers or underscore characters, equivalent to "/ [a-za-z0-9 _] /". W: Used to match all characters that do not match W, equivalent to "/ [^ a-za-z0-9 _] /". D: Used to match any non-10 credit numeric characters. : Used to match all characters outside of the resort line, if the modifier "S" is modified, "." Can represent any character. It is easy to express some cumbersome mode matching using the special characters above. For example, "// d0000 /" utilizes the above regular expression to match more than 10,000 integer strings. Positioning characters: Location characters is a very important character in the regular expression, and its main role is to describe the characters in the matching object.

^: Indicates that the matching mode appears in the beginning of the matching object (and "[]" inside ("[]") $: indicates that the matching mode appears on the end space of the matching object: indicates the two boundaries of the match and ends. One "/ ^ HE /": You can match the string starting with "HE" characters, such as Hello, Height, etc. "/ he $ /": You can match the string of strings that end up with "HE" characters, "" / HE / ": Sole, and ^ the role of ^, match the string starting with HE;" / he / ": The space is ended, like the role of $, match the string ended with HE;" / ^ he $ / ": Indicates that only the string" HE "matches. Brand: Regular expression In addition to user matching, you can also use parentheses "()" to record the required information, store it, read it later. For example: /^([A-ZA-Z0-9_-] )@ ([*-za-z0-9_-] ) (.[A-ZA-Z0-9_-]) $/ is the record email address Username, server address of the email address (in the form of username@server.com), after you want to read recorded strings, just need to read "Essential record order" take. For example, "/ 1" is equivalent to the first "[A-ZA-Z0-9 _-] ", "/ 2" is equivalent to the second ([A-ZA-Z0-9 _-] ), "/ 3 "is the third (. [A-ZA-Z0-9_-]). But in PHP, "/" is a special character, it needs to escape, so "" "" "// 1" should be written in the expression of PHP. Other Special Symbols: "|": or symbol "|" and php or the same, but a "|", rather than PHP's "||"! It is to be a character or another string, such as "/ abcd | dcba /" may match "ABCD" or "DCBA". 5 greedy mode: In the first character, "?" There is an important role, "greedy mode", what is "greed mode"? For example, we must match the string ending with the end of the letter "A", but the string that needs to be matched in "A" has many "B", such as "a bbbbbbbbbbbbbbbbbbbbbbbbb," A bbbbbbbbbbbbb ", the regular expression will match Is the first "B" or the last "B"? If you use greed mode, then you will match the last "B", and it is only matched to the first "B". The expression using greedy mode is as follows: /a. ?b/ /a. BU does not use greed mode as follows: /a. b/ The above uses a modifier U, see the section below.

6 Modifier: The modifier inside the regular expression can change many characteristics of the regularity, so that the regular expression is more suitable for your needs (Note: The modifier is sensitive to case, which means "e" is not equal " E "). The modifier in the regular expression is as follows: i: If "i" is added in the modifier, the general write sensitivity will be canceled, i.e., "A" and "A" are the same. M: The default positive start "^" and end "$" just for the regular string If you add "M" in the modifier, then the beginning and end will refer to each line of the string: the beginning of each line is "^ ", The end is" $ ". S: If you add "S" in the modifier, then the default "." represents any character other than the wrapper will become any character, that is, a wrapper! X: If you add this modifier, your blank characters in your expression will be ignored unless it has been essential. E: This modifier is only useful for Replacement, representing the PHP code in Replacement. A: If this modifier is used, the expression must be the beginning of the matching string. For example, "/ a / a" matches "ABCD". E: In contrast to "M", if you use this modifier, "$" will match the end of the absolute string, instead of the frontline, this mode is opened by default. U: The role of the question mark is similar to setting "greed mode". 7 PCRE-related regular expression functions: PHP's Perl compatible regular expressions provide multiple functions provided by pattern matching, replacement, and matching: 1, preg_match: function format: int preg_match (String Pattern, String Subject, Array [Matches]); this function will match the pattern expression in String, if [Regs] is given, String records in [Regs] [0], [regs] [1] represents the use of parentheses "()" Record the first string, [regs] [2] represents the second string recorded, and so on. PREG If you find a matching Pattern in String, you will return "true", otherwise it will return "false". 2, preg_replace: function format: Mixed preg_replace (Mixed Pattern, Mixed Replacement, Mixed Subject); This function uses all strings that match the String of Expression Pattern to express Replacement. If you need some characters that contain Pattern in Replacement, you can use "()" to record, just need to read it with "/ 1" in Replacement. 3, preg_split: function format: array preg_split (String Pattern, String Subject, INT [Limit]); Like the function split Complete Perl compatible regular expressions.

The third parameter LIMIT represents how many conformance values ​​are allowed. 4, preg_grep: function format: array preg_grep (String Patern, Array Input); This function and preg_match feature basically, but preg_grep can match all elements in the given array INPUT, return a new array. Let's take an example. For example, we have to check if the format of the email address is correct: '; if (! emailism (' Y10K @ fff ')) Echo' incorrect
';?> The above program will output "correct
incorrect". 8. PERL compatible regular expressions and Perl / EREG regular expressions in PERL / EREG: Although called "Perl compatible regular expression", PHP is still different, such as modifiers compared to PERL's regular expressions "G" represents all the match in Perl, but no support for this modifier is added in PHP. There is also the difference between the EREG series, EREG is also the regular expression function provided in PHP, but it is weak than PREG. 1, EREG does not need to use separator and modifier, so EREG's function is weaker than PREG. 2, about ".": Point in the regular is generally in addition to all characters other than the newline character, but in EREG "." Is any character, that is, a newline! If you want "in PREG". "" Can include a newline, and "s" can be added in the modifier. 3, EREG defaults to use greed mode, and cannot be modified, this brings trouble to many replacement and match.

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

New Post(0)