Use examples to learn Regular Expressions (1)-Basic Knowledge

Source: Internet
Author: User
Tags character classes
The basic syntax of a regular expression:

First, let's take a look at two special symbols: '^' and '$'. They indicate the beginning and end of a string. They are like this:

"^ The": corresponds to any string starting with ""
"Of despair $": the string ending with "of despair"
"^ Abc $": a string that starts and ends with "abc". It is "abc!
"Notice": a string containing "notice.

You can see that if you do not use either of the two symbols, just like in the last example, you are stating that you can match the style at any position of the string, that is to say, whether it appears in the header or tail.

What other symbols are '*', '+', and '? ', Which indicates the number of characters or strings. they mean: "0 or more (arbitrary)", "1 or more (at least 1 time)", and "0 or 1 time (at most 1 time) ". the following are some examples:

"AB *": corresponds to a string containing a followed by any B ("a", "AB", "abbb", etc .);
"AB +": similar, but at least one B ("AB", "abbb", etc .);
"AB? ": Either B or no;
"? B + $ ": the end part may have a or none, followed by more than one B.

You can also use curly brackets to indicate the range of the preceding characters:

"AB {2}": corresponds to a string containing two B ("abb") following;
"AB {2,}": contains at least 2 B ("abb", "abbbb", etc .);
"AB {3, 5}": 3 to 5 B ("abbb", "abbbb", or "abbbbb ").

Note that you must pay attention to the first number in the range. (For example, "{0, 2}", cannot be "{, 2 }"). at the same time, you may have noticed the characters '*', '+', and '? 'Is the same as "{0,}", "{1,}", and "{0, 1.

Now let's quantify some character sequences/small strings and put them in parentheses:

"A (bc) *": corresponds to a string containing any "bc" after;
"A (bc) {}": 1 to 5 "bc" can be.

The '|' character also acts as OR and is used to select:

"Hi | hello": corresponding to a string with "hi" or "hello;
"(B | cd) ef": A string with "bef" or "cdef;
"(A | B) * c": a string contains any combination of a and B and ends with a c;
A period ('.') indicates any individual character:

"A. [0-9]": a string that contains a character followed by a number;
"^. {3} $": A string with three characters.

Square brackets clearly indicate which characters can appear at a single character position:

"[AB]": corresponds to one a or one B (equivalent to "a | B ");
"[A-d]": a string has lowercase letters 'A' to 'D' (equivalent to "a | B | c | d" or even "[abcd]");
"^ [A-zA-Z]": a Start character is a string of English letters;
"[0-9] %": There is a string before the percent sign;
, [A-zA-Z0-9] $ ": A string ends with a comma followed by a number or letter.

You can use a list to remove the characters you don't want-just use a '^' in the first position in your square brackets (for example, "% [^ a-zA-Z] %" indicates that a character between two percentage signs is not an English letter ). in addition, you must note that in some cases, you do not need to add a backslash to indicate that special characters are invalid, for example, when the first position of the character class. for example, "($ | ¥) [0-9] +" can be expressed as ereg ("(/$ | ¥) [0-9] + ", $ str) (what string does this match ?)

Don't forget that all the special characters in square brackets will lose their special meanings (Note: '^ 'and'-'are exceptions), including backslash, for example, "[* +? {}.] "Is to match any of these symbols. regex man pages tells us that if a ']' is contained, you can place it at the first character, you can also put a backslash (for example,/[abc]/) in front of it.

Finally, I should mention the following details, such as collating sequences, character classes, and equivalence classes. I will not mention them any more, because it is not closely related to the depth of this article, you can find more in regex man pages.

From: http://se2k.51.net/myphp/

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.