Description of the relatively fine PHP regular learning examples _php skills

Source: Internet
Author: User
Tags character classes ereg
"^the": matches a string that starts with "the";

"Of despair$": matches a string ending with "of despair";

"^abc$": matches a string that begins with ABC and ends with ABC, and is actually only matched by ABC;

"Notice": matches a string containing notice;

You can see if you don't use the two characters we mentioned (the last example), which means that the pattern (regular expression) can appear anywhere in the string being tested, you don't lock him on either side.

There are also several characters ' * ', ' + ', and '? ', which are used to indicate the number of times a character can appear or order. They said: "Zero or more", "one or more", and "zero or a." Here are some examples:

"ab*": matches strings A and 0 or more B strings ("A", "AB", "abbb", etc.);

"ab+": Same as above, but at least one B ("AB", "abbb", etc.);

"AB": Match 0 or one B;

"a?b+$": a string that matches the end of one or 0 a plus more than one B.

You can also limit the number of characters appearing in curly braces, such as

"Ab{2}": Match a A followed by two B (one also can not be less) ("ABB");

"Ab{2,}": At least two B ("ABB", "abbbb", etc.);

"ab{3,5}": 2-5 B ("abbb", "abbbb", or "abbbbb").

You should also note that you must always specify (i.e, "{0,2}", not "{, 2}"). Again, you have to notice, ' * ', ' + ', and '? ' The three range callouts are the same, "{0,}", "{1,}", and "{0,1}" respectively.

Now put a certain number of characters in parentheses, for example:

"A (BC) *": Match a followed by 0 or a "BC";

"A (BC) {1,5}": one to 5 "BC."

There is also a character ' │ ', which is equivalent to an OR operation:

"Hi│hello": matches a string containing "hi" or "hello";

"(B│CD) EF": Matches a string containing "bef" or "cdef";

"(a│b) *c": matches a string containing such-multiple (including 0) A or B, followed by a string of C;

A point ('. ') Can represent all the single characters:

"A.[0-9]": A character followed by a number (a string containing such a string will be matched to omit this parenthesis later)

"^. {3}$: ends with three characters. The bracketed content matches only a single character

"[AB]": Match a single A or B (as with "a│b");

[a-d]: a single character that matches ' a ' to ' d ' (same as ' a│b│c│d ' and ' [ABCD] ' effect);

' ^[a-za-z] ': matches a string beginning with a letter

' [0-9]% ': matches a string containing x-percent

", [a-za-z0-9]$": matches a string with a comma at the end of a number or letter

You can also put the characters you don't want in the brackets, you just need to use ' ^ ' in the parentheses as the opening (i.e., "%[^a-za-z]%" matches contain two percent signs with a non-alphanumeric string).

In order to be able to explain, but "^. [$ () │*+? {\ "as a character with special meaning, you have to add" in front of these characters, and in php3 you should avoid using \, for example, regular expressions in the front of the pattern (\$│?[ 0-9]+ "should call Ereg (" \\$│?[ 0-9]+ ", $str) (Don't know if PHP4 is the same)

Don't forget that the characters inside the brackets are exceptions to this rule-within the brackets, all the special characters, including ('), will lose their special properties (i.e., "[*\+?{}.]" Matches a string containing these characters. And, as REGX's Handbook tells us: "If the list contains '," It's best to use it as the first character in the table (probably following ' ^ '). If it contains '-', it is best to put it at the front or end, or the second end of a range (i.e. [a-d-0-9] in the middle of '-' will be valid.

In order to be complete, I should involve collating sequences, character classes, buried equivalence classes. But I do not want to say in these areas too detailed, these in the following articles should not be involved. You can get more information from the Regex man pages.

How to build a pattern to match the amount of money entered

Well, now we're going to use what we've 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 represent money: "10000.00" and "10,000.00", or no decimal parts, "10000" and "10,000". Now let's start building this matching pattern:

^[1-9][0-9]*$

This is the variable that must begin with a number other than 0. But it also means that a single "0" cannot pass the test. Here's how to fix it:

^ (0│[1-9][0-9]*) $

"Only 0 matches the number that does not start with 0," we can also allow a minus sign to precede the number:

^ (0│-?) [1-9] [0-9]*) $

This is: "0 or an opening with 0 may have a minus sign in front of the number." Okay, okay, now let's not be so rigorous, let's start with 0. Now lets give up the minus sign because we don't need it when it comes to coins. We now specify the pattern to match the decimal part:

^[0-9]+ (\.[ 0-9]+)? $

This implies that the matching string must begin with at least one Arabic numeral. Note, however, that in the above mode "10." is not matched, only "10" and "10.2" can be. (Do you know why?)

^[0-9]+ (\.[ 0-9]{2})? $

We specified above the decimal point must have two decimal digits. If you think this is too harsh, you can change it to:

^[0-9]+ (\.[ 0-9]{1,2})? $

This will allow one to two characters after the decimal point. Now we add commas to increase readability (every three digits), and we can say this:

^[0-9]{1,3} (, [0-9]{3}) * (\.[ 0-9]{1,2})? $

Don't forget the plus sign ' + ' can be multiplication ' * ' If you want to allow a blank string to be entered (why?). Also don't forget that the backslash ' \ ' may have errors in the PHP string (a common mistake). Now that we can confirm the string, we're going to remove all the commas str_replace (",", "", $money) and then look at the type as a double and then we can do the math by him.

Construct regular expressions to check email

Let's continue to discuss how to verify an email address. There are three parts in a full email address: POP3 username (everything on the ' @ ' left), ' @ ', server name (that's the rest). User names can contain uppercase and lowercase Arabic numerals, periods ('. '), minus sign ('-'), and underline ('_'). The server name also complies with this rule, with the exception of the underscore.

Now, the start and end of a username cannot be a period. The same is true for servers. And you can't have two consecutive periods there's at least one character between them, okay now let's take a look at how to write a matching pattern for the user name:

^[_a-za-z0-9-]+$

It is not possible to allow a period to exist yet. We add it to:

^[_a-za-z0-9-]+ (\.[ _a-za-z0-9-]+) *$

The above means: "At least one canonical character (except. unexpected), followed by 0 or more strings starting with dots."

To simplify, we can replace Ereg () with eregi (). Eregi () is insensitive to case, we don't need to specify two ranges "A-Z" and "A-Z" – just specify one:

^[_a-z0-9-]+ (\.[ _a-z0-9-]+) *$

The following server name is the same, but to remove the underline:

^[a-z0-9-]+ (\.[ a-z0-9-]+) *$

Done. Now just use the "@" 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, only need to call

Eregi (' ^[_a-z0-9-]+ (\.[ _a-z0-9-]+) *@[a-z0-9-]+ (\.[ a-z0-9-]+) *$ ', $eamil)

You can get an email.

Other uses of regular expressions

Extract string

Ereg () and eregi () has an attribute that allows the user to extract part of the string through a regular expression (you can read the manual). For example, we want to extract the filename from Path/url – The following code is what you need:

Ereg ("([^\\/]*) $", $PATHORURL, $regs);
echo $regs [1];

High-level substitution

Ereg_replace () and Eregi_replace () are also useful: if we want to replace all the interval minus signs with commas:

Ereg_replace ("[\n\r\t]+", ",", Trim ($STR));

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.