First, let's take a look at two extraordinary characters: '^' and '$'. They are used to match the start and end of the string respectively. The following are examples:
"^ The": matches strings starting with ";
"Of despair $": match the string ending with "of despair;
"^ Abc $": matches strings starting with abc and ending with abc. In fact, only abc matches;
"Notice": matches a string containing notice;
You can see that if you didn't use the two characters we mentioned (the last example), that is, the pattern (Regular Expression) can appear anywhere in the string to be tested, you didn't lock him to either side.
How many characters are there, '*', ', and '? ', Which indicates the number or sequence of occurrences of a character. they indicate: "zero or more", "one or more", and "zero or one. "Here are some examples:
"AB *": matches strings a and 0 or more B ("a", "AB", "abbb", etc .);
"AB": Same as above, but at least one B ("AB", "abbb", etc .);
"AB? ": Matches 0 or 1 B;
"? B $ ": match the string ending with one or zero a plus more than one B.
You can also limit the number of characters in braces, such
"AB {2}": Match a and a with two B (one cannot be less) ("abb ");
"AB {2,}": at least two B ("abb", "abbbb", etc .);
"AB {3, 5}": 2-5 B ("abbb", "abbbb", or "abbbbb ").
You also need to pay attention to the fact that you must always specify (I. e, "{0, 2}", not "{, 2 }"). similarly, you must note that '*', ', and '? 'Are the same as the following three range Annotations: "{0,}", "{1,}", and "{0, 1 }".
Put a certain number of characters in parentheses, for example:
"A (bc) *": match a with 0 or a "bc ";
"A (bc) {}": one to five "bc ."
There is also a character '│', which is equivalent to the OR operation:
"Hi │ hello": Match string containing "hi" or "hello;
"(B │ cd) ef": matches strings containing "bef" or "cdef;
"(A │ B) * c": match a string that contains multiple (including 0) a or B strings followed by a c;
A point ('.') can represent all single characters:
"A. [0-9]": a string with a character and a number (a string containing such a string will be matched, and this bracket will be omitted later)
"^. {3} $": ends with three characters. The content enclosed in brackets only matches a single character.
"[AB]": Match a or B (same as "a │ B );
"[A-d]": match a single character from 'A' to 'D' (same effect as "a │ B │ c │ d" and "[abcd );
"^ [A-zA-Z]": matches a string starting with a letter.
"[0-9] %": match a string containing x %
", [A-zA-Z0-9] $": match a string ending with a comma plus a number or letter
You can also column the characters you don't want in brackets. You just need to use '^' in the brackets to start with (I. e ., "% [^ a-zA-Z] %" matches a non-letter string with two percentage signs ).
To be able to explain, but "^. [$ () │ *? {"You must add'' in front of these characters as special characters, and avoid using it at the very beginning of the pattern in php3, for example, regular Expression "($ │? [0-9] "ereg (" ($ │? [0-9] ", $ str) (I don't know if php4 is the same)
Do not forget that the characters in brackets are exceptions of this rule.