Home  >  Article  >  Backend Development  >  Learn regular expressions with examples in PHP

Learn regular expressions with examples in PHP

WBOY
WBOYOriginal
2016-07-29 08:35:47984browse

Learn regular expressions by looking at examples
First, let us look at two special characters: '^' and '$'. They are used to match the beginning and end of a string respectively. Here are examples:
First, let us look at Look at two special characters: '^' and '$'. They are used to match the beginning and end of the string respectively. Here are examples:
"^The": Matches the string starting with "The";
" of despair$": matches the string ending with "of despair";
"^abc$": matches the string starting with abc and ending with abc, in fact, only abc matches it;
"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 to say, the pattern (regular expression) can appear anywhere in the string being tested, and you do not use it Lock to the sides.
There are also several characters '*', '+', and '?', which are used to represent 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 a string consisting of 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 one Or a string ending with 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 (a No less) ("abb");
"ab{2,}": at least two more 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)*": matches a followed by 0 or one "bc";
"a(bc){1,5}": one to 5 "bc."
There is also one character '│', which is equivalent to the OR operation:
"hi│hello": matches strings containing "hi" or "hello";
"(b│cd)ef" : Matches strings containing "bef" or "cdef";
"(a│b)*c": Matches strings containing - multiple (including 0) a or b, followed by a c string ;
A dot ('.') can represent 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 ( and "a │b" Same);
"[a-d]": Matches a single character from 'a' to 'd' (same effect as "a│b│c│d" and "[abcd]");
"^[ a-zA-Z]": Matches strings starting with letters
"[0-9]%": Matches strings containing x%
",[a-zA-Z0-9]$": Matches A string ending with a comma plus a number or letter
You can also list the characters you don’t want in brackets, you just need to use '^' as the beginning inside the brackets (i.e., "%[^a- zA-Z]%" matches a string containing two percent signs with a non-letter inside).
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 it is the same in php4)
Don’t forget that the characters inside the square brackets are exceptions to this rule - inside the square brackets, all special characters, Including (''), will lose their special properties (i.e., "[*+?{}.]" matches strings containing these characters). Also, as the regx manual tells us: "If the list contains ']', it is best to put it as the first character in the list (maybe after '^'). If it contains '-', it is best to put it at the front or last, or or the first character of a range Two ending points (i.e. [a-d-0-9] with a '-' 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 on these aspects. , none of this needs to be covered in the following article. 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 what we have learned. 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. The following is the solution:
^(0│[1-9] [0-9]*)$
"Only 0 and numbers not starting with 0 match", we can also allow a negative sign before the number:
^(0│-?[1-9][0- 9]*)$
This is: "0 or a number starting with 0 that may have a negative sign in front of it." Okay, okay now let's not be so strict and allow starting with 0. Now let's drop the negative sign , because we don’t need to use it when representing coins. We now specify the pattern to match the decimal part:
^[0-9]+(.[0-9]+)?$
This implies the matching string It must start with at least one Arabic numeral. But note that 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 '' can cause errors in PHP strings (a very common mistake). Now that we can validate the string, we Now remove all the commas str_replace(",", "", $money) and then treat the type as double and we can do mathematical calculations with it.
Construct a regular expression to check email
Let's continue to discuss how Verify an email address. There are three parts in a complete email address: POP3 username (everything to the left of '@'), '@', server name (that's the rest). Usernames can contain uppercase and lowercase letters Arabic numerals, periods ('.'), minus signs ('-'), and underscores ('_'). The server name also conforms to this rule, except of course the underscore.
Now, the beginning and end of the user name cannot be a period . The same goes for the server. And you can't have two consecutive periods with at least one character between them. Now let's look at how to write a matching pattern for the username:
^[_a-zA-Z0-9-] +$
The existence of periods is not allowed yet. We add it:
^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$
The meaning of the above That is to say: "Start with at least one canonical character (except . unexpected), followed by 0 or more strings starting with a dot."
To simplify it a bit, we can use eregi() instead of ereg().eregi() Being case-insensitive, we don’t need to specify two ranges "a-z" and "A-Z" – we only need to specify one:
^[_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 "@" connects the two parts:
^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9 -]+)*$
This is the complete email authentication matching mode, 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 from path/URL Extract file name – 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 separated negative signs with commas:
ereg_replace("[ ]+", ",", trim($str));

The above has introduced how to learn regular expressions in PHP by looking at examples, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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