Home > Backend Development > PHP Tutorial > Learn regular expressions with examples in PHP_PHP Tutorial

Learn regular expressions with examples in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:58:36
Original
872 people have browsed it

Learn regular expressions by looking at examples
First of all, let us look at two special characters: '^' and '$'. They are used to match the beginning and end of strings respectively. Here are examples:

First, let’s take a look at two special characters: '^' and '$'. They are used to match the beginning and end of a string respectively. Here are some examples:

"^The": Matches strings starting with "The";

"of despair$": matches strings ending with "of despair";

"^abc$": matches strings starting with abc and Strings ending with abc are actually only matched by abc;

"notice": matches strings containing notice;

You can see if you don't use what we mentioned Two characters (the last example) means that the pattern (regular expression) can appear anywhere in the string being tested, you are not locking it to both sides.

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

"ab*": Matches the string a and 0 or more b ("a", "ab" , "abbb", etc.);

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

"ab? ": Matches 0 or one b;

"a?b+$": Matches a string ending with one or 0 a plus one or more b.

You can also Limit the number of characters appearing in curly brackets, for example

"ab{2}": matches an a followed by two b (no 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}"). Likewise, You must notice that '*', '+', and '?' are the same as the following three range annotations, "{0,}", "{1,}", and "{0,1}" respectively .

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

"a(bc)*": matches a followed by 0 or one "bc";

"a(bc){1,5}": one to 5 "bc."

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

"hi│ hello": matches strings containing "hi" or "hello";

"(b│cd)ef": matches strings containing "bef" or "cdef";

"(a│b)*c": Matches a string containing - multiple (including 0) a or b, followed by a string of c;

A dot ('.') can Represents all single characters:

"a.[0-9]": an a followed by a character followed by a number (strings containing such a string will be matched, this bracket will be omitted in the future)

"^.{3}$": ends with three characters. The content enclosed in square brackets only matches a single character

"[ab]": matches a single a or b (same as "a│b");

"[a-d]": matches a single character from 'a' to 'd' (same as "a│b│c│d" and "[abcd ]"The effect is the same);

"^[a-zA-Z]": Matches a string starting with a letter

"[0-9]%": Matches a string of the form x % string

",[a-zA-Z0-9]$": Matches a string ending with a comma plus a number or letter

You can also put what you don’t want The characters are listed in brackets, you only need to use '^' as the beginning of the bracket (i.e., "%[^a-zA-Z]%" matches two percent signs with a non-letter character string).

In order to be able to explain, but when "^.[$()│*+?{" is used as a character with special meaning, you must add '' in front of these characters, and in In php3 you should avoid using it at the beginning of the pattern. For example, the regular expression "($│?[0-9]+" should be called ereg("($│?[0-9]+", $str ) (I don’t know if php4 is the same)

Don’t forget that the characters inside the brackets are an exception to this rule - inside the brackets, all special characters, including (''), will lose them special properties (i.e., "[*+?{}.]" matches strings containing these characters). Also, as the regx manual tells us: "If the list contains ']', it is best to treat it as a list the first character in (may follow '^'). If it contains '-', it is best to put it at the front or at the end, or at the second end point of a range (i.e. [a-d-0 -9] The '-' in the middle will be valid.

For completeness, I should cover collating sequences, character classes, and equivalence classes. But I don't want to go into too much detail in these aspects, which are below This article doesn't need to be covered. You can get more information in the regex man pages.

How to build a pattern to match the currency amount input

Okay, now we are going to use our Use what you learned to do something useful: construct a matching pattern to check whether the input information is a number representing money.We think there are four ways to represent the amount of money: "10000.00" and "10,000.00", or without a decimal part, "10000" and "10,000". Now let's start building this matching pattern:

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

This means that all variables must start with a non-0 number. But this also means that a single "0" cannot pass the test. Here is the solution:

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

"Only 0 and numbers not starting with 0 match", we can also allow a negative Before the number:

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

This is: "0 or a number starting with 0 may have a negative The number with the sign in front of it." Okay, okay now let's be less strict and allow it to start with 0. Now let's drop the minus sign , since we don't need it when representing coins. We now specify the pattern to use Match the decimal part:

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

This implies that the matched string must start with at least one Arabic digit. But note , in the above pattern "10." is not matched, only "10" and "10.2" are acceptable. (Do you know why)

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

We specified above that there must be 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 or two characters after the decimal point. Now we add commas (every third digit) to increase readability, We can express it like this:

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

Don't forget that the plus sign '+' can be replaced by the multiplication sign '*' if you want to allow blank strings to be entered (why?). Also don't forget that the backslash ''' may be used in PHP strings An error occurred (a very common error). Now that we can confirm the string, we can now remove all commas str_replace(",", "", $money) and then treat the type as double and we can pass He did the math.

Constructing a regular expression to check email

Let’s continue discussing how to verify an email address. There are three parts in a complete email address: POP3 Username ( Everything to the left of '@'), '@', the server name (that's the rest). Usernames can contain uppercase and lowercase letters, Arabic numerals, periods ('.'), minus signs ('-'), and underscores ('_'). Server names also comply with this rule, except of course the underscore.

Now, usernames cannot start and end with periods. The same goes for servers. And you cannot have two consecutive periods. There is at least one character between them, so now let’s take a look at how to write a matching pattern for the username:

^[_a-zA-Z0-9-]+$

Now The existence of periods is not allowed. We add it:

^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$

The above means: "Start with at least one standard character (except . and unexpected), followed by 0 or more strings starting with a dot."

To simplify it a bit, we can use eregi () replaces ereg().eregi() which is case insensitive, we don’t need to specify two ranges "a-z" and "A-Z" – only one needs to be specified:

^[_a- z0-9-]+(.[_a-z0-9-]+)*$ The server name after

is the same, but the underscore must be removed:

^[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 complete Email authentication matching pattern, just call

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

to get whether it is email.

Other uses of regular expressions

Extracting strings

ereg() and eregi() have a feature that allows users to extract 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 substitution

ereg_replace() and eregi_replace() are also very useful: if we want to All spaced negative signs are replaced with commas:

ereg_replace("[ ]+", ",", trim($str));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317550.htmlTechArticleLook at examples to learn regular expressions. First, let us look at two special characters: '^' and '$ 'They are used to match the beginning and end of the string respectively. Here are examples: First, let...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template