Basic and simple examples of PHP regular expressions

Source: Internet
Author: User
Writing regular expressions is often a headache, and it may be that the regular expression you just wrote will not be understood for a while. This article describes the regular basic syntax and simple PHP code examples that make it easier for friends to read through when they write regular expressions.

Start with ^ and end of $ let PHP check from the beginning of the string to the end. If there is no $, the program will still match to the end.

[and] are used to restrict the type of license input. For example A-Z allows all lowercase letters, a-Z allows all uppercase letters, 0-9 all numbers, and more, among other types.

{and} are used to limit the number of characters expected. For example, {2,4} indicates that each section of a string can have a length of 2-4 characters, such as. com.cn or. Info. Here, "." does not count as a character, because the type of license input defined before {2,4} is only uppercase and lowercase, so the segment matches only the uppercase and lowercase letters

(and) are used to combine subsections and define the characters that must exist in the string. (A|b|c) to match A or B or C.

(.) All characters are matched, and [.] Matches only "." itself.

To use some of the symbols themselves, you must add a \ to the front. These characters are: () []. * ? + ^ | $

Attached: PCRE Grammar Guide

/delimiter
^ String Header
$ string Tail
[A-z] all lowercase letters
[A-z] all uppercase letters
[0-9] All numbers
? 0 or a immediately preceding character
* 0 or more immediately preceding characters
+ one or more immediately preceding characters
{4} 4 immediately preceding characters
{4,8} 4-8 immediately preceding characters
. Any character
(Red|green|blue) Red or green or blue
S space

Special characters (required in front plus \)
( ) [ ] . * ? + ^ | $

Rule Matching Preg_match

With Preg_match (), we can complete the rule matching of strings. If a match is found, the Preg_match () function returns 1, otherwise 0 is returned. There is also an optional third parameter that allows you to put a matching part in an array. This functionality can become very useful when validating data.
$string = "Football";
if (Preg_match ('/foo/', $string)) {
Match correct
}

Rule substitution preg_replace

Preg_replace allows you to replace a regular expression in a string that matches your definition.
$val = "/*123456789*/ABCD";
$PP = Preg_replace ("/[(\/\*) + (.) (\*\/) +]+/", ' fuck ', $val);
Print_r ($PP);
The result is
Fuck123456789fuckabcd

Rule Segmentation Preg_split

Preg_split can divide the entire string into multiple segments of 1, 2, or more characters by matching regular expressions. For example, to get a label, whether it is separated by a space or a comma:
$tags = Preg_split ('/[,]/', ' my,tags,unevenly,spaced ');
Print_r ($tags);
The result is:
Array ([0] = my [1] = tags [2] = = unevenly [3] = spaced)


Related recommendations:

The most complete PHP regular expression in history-regular expressions

A detailed description of the Preg_match_all function in PHP regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.