A lot of people are not familiar with the regular expression, especially from the www.phpuser.com reprint about the expression of PHP, in fact, and ASP

Source: Internet
Author: User
Tags regular expression
Regular Expressions in PHP (i)
Hunte April 14, 2000
Email it to me. I'll talk about my opinion. Last article

PHP inherits *nix's consistent tradition of fully supporting formal expression processing. Regular expressions provide an advanced, but not intuitive, method for string matching and processing. Friends who have used regular expressions in Perl know that regular expressions are powerful, but not as easy to learn.
Like what:
^.+@.+.. +$
This powerful but incomprehensible code is enough to make some programmers headache (I am) or let them give up using regular expressions. Believe that when you finish this tutorial, you can understand the meaning of this code.
Basic Pattern Matching
Everything starts from the basics. Patterns, the most basic element of regular expressions, are a set of characters that describe the character of a string. Patterns can be simple, composed of ordinary strings, and can be very complex, often using special characters to represent a range of characters, repeat occurrences, or represent contexts. For example:
^once
This pattern contains a special character ^, which indicates that the pattern matches only those strings that start with once. This pattern, for example, matches the string "Once Upon a Time" and does not match the "There once was a mans from NewYork". Just as the ^ symbol indicates the beginning, the $ symbol is used to match the strings that end in the given pattern.
bucket$
This pattern matches the "who kept all of the this cash in a bucket" and does not match "buckets". Characters ^ and $ are used at the same time to represent exact matches (strings are the same as patterns). For example:
^bucket$
Matches only the string "bucket". If a pattern does not include ^ and $, it matches any string that contains the pattern. For example: mode
Once
and string
There once was a mans from NewYork
Who kept all of the cash in a bucket.
is a match.
The letters in the pattern (O-N-C-E) are literal characters, that is, they represent the letter itself, and the number is the same. Some other slightly more complex characters, such as punctuation and white space (spaces, tabs, and so on), use the escape sequence. All escape sequences are preceded by a backslash (). The escape sequence for a tab is:. So if we're going to detect whether a string starts with tabs, you can use this pattern:
^
Similarly, a carriage return is represented by a "new line". Other special symbols, which can be preceded by a backslash, such as the backslash itself with the expression, the period.
Character clusters
In programs on the Internet, regular expressions are typically used to validate user input. When a user submits a form, it is not enough to determine whether the phone number, address, email address, credit card number, etc. are valid, and the usual literal characters are not sufficient.
So to use a more liberal way of describing the pattern we want, it's a character cluster. To create a character cluster that represents all the vowel characters, put all the vowel characters in one square bracket:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.