"^ 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 do not 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 must also note 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 "^. [$ () │ * +? When {\ "is a special character, you must add'' in front of these characters, and in php3, you should avoid using \ at the beginning of the pattern, 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-in brackets, all special characters, including (''), will lose their special properties (I. e ., "[* \ +? {}.] "Match strings containing these characters ). also, as the regx manual tells us: "If the list contains ']', it is best to use it as the first character in the list (possibly following '^ ). if it contains '-', it is best to put it at the beginning or the end, or the second end point of a range (I. e. the '-' in the [a-d-0-9] will be valid.
For completeness, I should involve collating sequences, character classes, and equivalence classes. however, I do not want to elaborate on these aspects, and these articles do not need to be involved. you can get more messages in regex man pages.
How to build a pattern to match the number of currency Input
Now let's use what we have learned to do something useful: Build a matching pattern to check whether the input information is a number that represents money. We think there are four ways to indicate the number of money: "10000.00" and "10,000.00", or no decimal part, "10000" and "10,000 ". now let's start building this matching mode:
^ [1-9] [0-9] * $
This variable must start with a number other than 0, but it also means that a single "0" cannot pass the test. The following is a solution:
^ (0 │ [1-9] [0-9] *) $
"Only numbers starting with 0 and not 0 match", we can also allow a negative number before the number:
^ (0 │ -? [1-9] [0-9] *) $
This is: "0 or a digit starting with 0 may have a negative number in front. "Well, now let's not be so rigorous. We can start with 0. now let's give up the negative number, because we don't need to use it to represent coins. we now specify a pattern to match the fractional part:
^ [0-9] + (\. [0-9] + )? $
This implies that the matched string must start with at least one Arabic number. but note that in the above mode, "10. "It does not match. Only" 10 "and" 10.2 "are allowed. (Do you know why)
^ [0-9] + (\. [0-9] {2 })? $
We have specified two decimal places. If you think this is too harsh, you can change it:
^ [0-9] + (\. [0-9] {1, 2 })? $
This will allow one or two decimal places. Now we add a comma (every three digits) to increase readability, which can be expressed as follows:
^ [0-9] {1, 3} (, [0-9] {3}) * (\. [0-9] {1, 2 })? $
Do not forget the plus sign '+' to be multiplied by '*'. If you want to allow blank strings to be input (Why ?). Do not forget that the backslice bar '\' may cause errors in the php string (common errors ). now we can confirm the string. Now we can remove all the commas (,) from str_replace (",", "", $ money) then we can regard the type as double and then use it for mathematical computation.
Construct a regular expression for checking email
Let's continue to discuss how to verify an email address. there are three parts in a complete email address: POP3 User Name (everything on the left of '@'), '@', and server name (that is, the remaining part ). the user name can contain uppercase and lowercase letters, Arabic numerals, periods ('. '), minus sign ('-'), and underline ('_'). the server name also complies with this rule, except for the underlines.
The start and end of the user name cannot be a period. the same is true for servers. you cannot have at least one character between two consecutive periods. Now let's take a look at how to write a matching pattern for the user name:
^ [_ A-zA-Z0-9-] + $
The end cannot exist yet. We can add the following:
^ [_ A-zA-Z0-9-] + (\. [_ a-zA-Z0-9-] +) * $
The above means: "There is at least one canonicalized character (except. Unexpected), followed by 0 or more strings starting with a point ."
To simplify it, we can use eregi () to replace ereg (). eregi () is case-insensitive and we don't need to specify two ranges: "a-z" and "A-Z"-Just specify one:
^ [_ A-z0-9-] + (\. [_ a-z0-9-] +) * $
The server name is the same, but the underline should be removed:
^ [A-z0-9-] + (\. [a-z0-9-] +) * $
Done. Now you only need to use @ to connect the two parts:
^ [_ A-z0-9-] + (\. [_ a-z0-9-] +) * @ [a-z0-9-] + (\. [a-z0-9-] +) * $
This is the complete email authentication matching mode. You only need to call
Eregi ('^ [_ a-z0-9-] + (\. [_ a-z0-9-] +) * @ [a-z0-9-] + (\. [a-z0-9-] +) * $ ', $ eamil)
Then you can check whether the email is used.
Other regular expressions
Extract string
Ereg () and eregi () has a feature that allows users to extract a part of a string through regular expressions (you can read the manual for specific usage ). for example, we want to extract the file name from path/URL-the following code is what you need:
Ereg ("([^ \/] *) $", $ pathOrUrl, $ regs );
Echo $ regs [1];
Advanced replacement
Ereg_replace () and eregi_replace () are also very useful: if we want to replace all the negative signs at intervals with commas:
Ereg_replace ("[\ n \ r \ t] +", trim ($ str ));