Example of _php regular expression in PHP

Source: Internet
Author: User
Tags character classes ereg
Look at the case study regular expression
First, let's take a look at two special characters: ' ^ ' and ' $ ' they are used to match the start and end of the string, respectively, to illustrate the following:

First, let's take a look at two special characters: ' ^ ' and ' $ ' they are used to match the start and end of the string, respectively, to illustrate the following:

"^the": matches a string that begins with "the";

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

"^abc$": matches the string starting with ABC and ending with ABC, in fact only ABC matches it;

"Notice": matches the string containing the notice;

You can see if you're not using the two characters we mentioned (the last one), which means that the pattern (regular expression) can appear anywhere in the checked string, and you don't lock him to either side.

There are also several characters ' * ', ' + ', and '? ', which they use to indicate the number or order in which a character can appear. They say, "zero or more", "one or more", and "zero or a." Here are some examples:

"ab*": matches strings A and 0 or more B consisting of a string ("a", "AB", "abbb", etc);

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

"AB": Match 0 or one B;

"a?b+$": matches a string that ends with one or 0 a plus more than one B.

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

"Ab{2}": Match one A followed by two B (one is not less) ("ABB");

"Ab{2,}": Minimum of two B ("ABB", "abbbb", etc);

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

You also have to notice that you must always specify (i.e, "{0,2}", not "{, 2}"). Again, you have to notice, ' * ', ' + ', and '? ' the same as three range labels, "{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 ' │ ', equivalent to 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 C string;

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

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

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

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

"[A-d]": a single character matching ' a ' to ' d ' (same as "a│b│c│d" and "[ABCD]");

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

"[0-9]%": matches a string containing the form x percent

", [a-za-z0-9]$": matches a string that ends with a comma in addition to a number or letter

You can also put the words you don't want to be in brackets, you just need to use ' ^ ' as the opening in the total brackets (i.e., "%[^a-za-z]%" matches two percent of the sign containing a non-alphabetic string).

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

Do not forget that the characters inside the brackets are exceptions to this rule-in brackets, all special characters, including (' '), will lose their special properties (i.e., "[*+?{}.]" Matches a string containing these characters). Also, as REGX's Handbook tells us: "If the list contains '] ', it is best to use it as the first character in the table (possibly following the ' ^ '). If it contains '-', it is best to place it on the front or the last side, or the second end of a range (i.e. [a-d-0-9] in the middle of the '-' will be effective.

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

How to build a pattern to match the input of a currency quantity

Well, now we're going to use what we've learned to do something useful: build a matching pattern to check whether the information entered is a number that represents money. We think that there are four ways to represent money: "10000.00" and "10,000.00", or there are 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 start 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 and numbers not starting with 0 match", we can also allow a minus sign before the number:

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

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

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

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

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

We have to specify two decimal places after the decimal point. 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 a comma for readability (every three bits), so we can say:

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

Don't forget that the plus ' + ' can be replaced by multiplication sign ' * ' If you want to allow blank strings to be entered (why?). Also do not forget that the backslash "may have errors (very common errors) in the PHP string. Now that we can confirm the string, we now take all the commas out of Str_replace (",", "", $money) and then treat the type as a double and we can do the math with him.

Constructs a regular expression to check e-mail

Let's continue to discuss how to verify an email address. There are three parts in a full email address: The POP3 username (everything on the left of ' @ '), ' @ ', the server name (the rest of the section). The user name can contain uppercase and lowercase Arabic numerals, a period ('. '), minus ('-'), and an underscore ('_'). The server name also conforms to this rule, except of course the underscore.

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

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

There is no time to allow the period to exist. We add it to:

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

This means: "At least one canonical character (except. Unexpected) begins, followed by 0 or more strings starting with a dot."

To make it simpler, we can replace Ereg () with eregi (). Eregi () is not case sensitive, we do not 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 underscore:

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

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

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

You will be able to get an email.

Other uses of regular expressions

Extracting a string

Ereg () and eregi () have a feature that allows a user to extract a portion of a string from a regular expression (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];

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 ("[]+", ",", 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.