PHP Programming Tips: Learn Regular Expressions with Examples_PHP Tutorial

WBOY
Release: 2016-07-13 17:32:37
Original
888 people have browsed it

"^The" : Match the string starting with "The";

 "of despair$": Match the string ending with "of despair";

 "^abc$": Match the string starting with abc And the string ending with abc, actually only abc matches it;

  "notice": matches the string containing notice;

You can see if you don't use what we mentioned The two characters (last example) mean that the pattern (regular expression) can appear anywhere in the string being tested, you don't lock it to both sides.

There are several characters *, +, 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 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?": Match 0 or a b;

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

You can also limit characters inside curly braces The number of occurrences, 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 must also note that you must always specify (i.e., "{0,2}", not "{,2}"). Likewise, you must note, * , +, 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 one 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 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, and 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" is the same);

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

 "^[a-zA-Z]": Matches strings starting with letters

"[0-9]%": Matches strings containing x%

http://www.bkjia.com/PHPjc/508679.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508679.htmlTechArticleFirst, let us look at two special characters: '^' and ''. They are used to match respectively. The beginning and end of the string are explained below with examples: "^The": Matches words starting with "The"...
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