Home  >  Article  >  Backend Development  >  Detailed explanation of PHP regular learning examples_PHP tutorial

Detailed explanation of PHP regular learning examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:50:32805browse

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

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

"^abc$": Matches strings that start with abc and end with abc. In fact, only abc matches it;

"notice": Matches strings containing notice;

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

There are several characters here, '*', '+', and '?', which are used to represent the number or order of occurrences of a character. 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, such as

"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 into parentheses, for example:

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

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

There is also a character '│', 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 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 the sake of 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 even 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're 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 numeral. But note , in the above pattern "10." does not match, only "10" and "10.2" can. (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 for checking 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. unexpected), followed by 0 or more strings starting with a dot."

To simplify it, 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 following server name is the same, but the underscore must be removed:

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

Done. Now you just need to 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)

You can 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 substitutions

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319343.htmlTechArticle"^The": matches a string starting with "The"; "ofdespair$": matches a string starting with "ofdespair "String ending with "^abc$": Matches strings starting with abc and ending with abc. In fact, only abc matches it...
Statement:
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