Regular expression in PHP (1)
Source: phpuser.com PHP inherits * NIX's consistent tradition, fully supports processing of regular expressions. Regular expressions provide advanced, but not intuitive string matching and processing methods. Friends who have used Perl know that the functionality of formal expressions is very powerful, but it is not so easy to learn. such as:
^. @. / .. $ This is a valid code that is difficult to understand enough to make some programmers headache (I just) or let them give up using regular expressions. I believe that after you finish this tutorial, you can understand the meaning of this code. Basic patterns match everything from the most basic beginning. Mode, is the most basic element of regular expression, which is a set of characters describing string feature. The pattern can be very simple, consisting of ordinary strings, can also be very complicated, often representing a range of characters in a range, repeated, or represents the context with a special character. E.g:
^ The ONCE mode contains a special character ^, indicating that the mode only matches those strings starting with ONCE. For example, this mode matches the string "ONCE UPON A TIME", which does not match "There Once Was A Man from NewYork. As the symbol indicates the beginning, the $ symbol is used to match those strings ending at a given mode.
BUCKET $ This mode matches "WHO Kept All of this Cash in A Bucket" does not match "BUCKETS". When using characters ^ and $ simultaneously, indicate exact match (the string is the same as mode). E.g:
^ Bucket $ Match string "bucket". If a pattern does not include ^ and $, then it matches any string containing the mode. For example: mode
Once and string There once Was a man from newyorkwho kept all of his cash in a bucket. It is matched. The letter (O-N-C-E) in this mode is a literal character, that is, they indicate that the letter itself, the number is also the same. Others have some slightly complex characters such as punctuation and white characters (spaces, tabs, etc.) to use escape sequences. All escape sequences are headed in a backslash (). The escape sequence of the tab is: T. So if we want to detect if a string starts with the tab, you can use this mode:
^ T is similar, use n to indicate "new row", and r is reply. Other special symbols can be used in front of the backslash, such as the anti-slant bar itself with / indication, the number. Due to this class. Character clusters In the Internet program, regular expressions are usually used to verify the user's input. After the user submits an Form, it is necessary to determine whether the input phone number, address, email address, credit card number, etc. are valid, and use ordinary literally based characters. So use a more freely describing the way we want, it is a character cluster. To create a character cluster that represents all vow characters, put all the vow characters in a square bracket:
[AAeeiIoouu] This mode matches any flyer character, but only one character can be represented. Use a linked font size to represent a range of characters, such as:
[AZ] // Match all lowercase letters [AZ] // match all uppercase letters [A-ZA-Z] // match all letters [0-9] // match all numbers [0-9.- ] // Match all numbers, period, and minus [FRTN] // match all white characters, which also represents a character, which is a very important. If you want to match a string consisting of a lowercase letter and a digit, such as "Z2", "T6" or "G7", but not "AB2", "R2D3" or "B52", with this mode: ^ [AZ] [0-9] $ although [AZ] represents the range of 26 letters, but here it can only match the first character is a string of lowercase letters. The front mentioned that ^ indicates the beginning of the string, but it still has another meaning. When using ^ in a set of square brackets, it means "non-" or "exclusion", often used to eliminate a character. In the previous example, we ask the first character that cannot be numbers:
^ [^ 0-9] [0-9] $ This mode is matched with "& 5", "G7", "-2", but does not match "12", "66". Here is a few examples of exclusion of specific characters:
[^ AZ] // In addition to all characters other than lowercase letters [^ // ^] // except "/" and "^" characters, all characters outside of the "^" characters, in addition to double quotes (") and orders All characters outside the quotation marks (') "." (Point, junctions) are used in regular expressions to represent all characters except "new rows". So the mode "^ .5 $" matches any two characters, ending with the number 5 and the other non-"new row" characters. Mode "." You can match any string, except for a string and only a "new row" string. PHP's regular expression has some built-in general character clusters, the list is as follows:
Character cluster enlightenment [[: alpha:]] Any letter [[: DIGIT:]] [[: alnum:]] any letters and numbers [[: space:]] any white characters [[: Upper:]] Underllis letters [[: Lower:]] Any lowercase letters [[: punct:]] Any punctuation [[: xdigit:]] Any 16-based number, equivalent to [0-9A-FA-F]