Home Backend Development PHP Tutorial PHP Programming Tips: Learn Regular Expressions with Examples_PHP Tutorial

PHP Programming Tips: Learn Regular Expressions with Examples_PHP Tutorial

Jul 13, 2016 pm 05:32 PM
php and character study Example us regular look Programming skills expression first

"^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"...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles