Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of PHP regular expression

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of the sample analysis of PHP regular expressions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Mind map

Introduction

Regular expressions should be often used in development. Now many development languages have regular expression applications, such as JavaScript, Java, .net, PHP and so on. Today, I will chat with you about my understanding of regular expressions. Please give me more advice on what is wrong!

Delta delimiter

Delta character field

Delta modifier

Delta qualifier

Delta character

Delta back reference

Δ inert matching

Delta annotation

Δ zero character width

Positioning

When do we use regular expressions? It would be nice not to use regularities for all character operations. Php uses regularities in some ways but affects efficiency. When we encounter the parsing of complex text data, using regularity is a better choice.

Advantages

Regular expressions can improve productivity and save you code to a certain extent when dealing with complex character operations.

Shortcoming

When we use regular expressions, complex regular expressions increase the complexity of the code, which is difficult to understand. So we sometimes need to add comments inside the regular expression.

General mode

The "delimiter", which usually uses "/" as the beginning and end of the delimiter, can also use "#".

When should I use "#"? This is usually when you have a lot of "/" characters in your string, because regular characters need to be escaped, such as uri.

$regex ='/ ^ http:\ / ([\ w.] +)\ / ([\ w] +)\ / ([\ w] +)\ / ([\ w] +)\ .html $/ itransitive str = 'http://www.youku.com/show_page/id_ABCDEFG.html';$matches = array (); if (preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n"

The $matches [0] in preg_match will contain a string that matches the entire pattern.

$regex ='# ^ http://([\w.]+)/([\w]+)/([\w]+)\.html$#i';$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';$matches = array (); if (preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n"

The last "I" in the ('/ ^ http:\ / ([\ w.] +)\ / ([\ w] +)\ / ([\ w] +)\ / ([\ w] +)\ .html / i') is the modifier to ignore case, and another thing we often use is "x" to ignore spaces.

Contribution Code:

$regex ='/ HELLO/';$str = 'hello word';$matches = array (); if (preg_match ($regex, $str, $matches)) {echo' No i:Valid successful completion, "\ n";} if (preg_match ($regex.'i', $str, $matches)) {echo 'YES i:Valid successful completion, "\ n";}

Character field: [\ w] the part that is expanded with square brackets is the character field.

Qualifier: the symbols after [\ w] {3jue 5} or [\ w] * or [\ w] + all indicate qualifiers. Now introduce the specific significance.

{3,} more than 3 characters, {, 5} up to 5 characters, {3} three characters.

* indicates 0 to more

+ represents one or more.

Dropout symbol

^:

> put it in the character field (e.g. [^\ w]) to indicate negation (excluding)-- "reverse selection"

Put it before the expression to start with the current character. (/ ^ nplink I, which starts with n).

Note that we often call "\"jump characters". Used to escape special symbols, such as ".", "/"

Wildcard (lookarounds): asserts the existence of certain characters in some strings!

There are two types of lookarounds: lookaheads (forward pre-check? =) and lookbehinds (reverse pre-check? Format:

Forward pre-check: (? =) corresponding to (?!) Express a negative intention

Reverse pre-check: (? $regex ='/ (? chuanshanjia) [\ s] Is [\ s] (? P=author) / author:chuanshanjia Is chuanshanjia';$matches = array (); if (preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n"

Running result

Lazy matching (remember: two operations will be performed, see the principle section below)

Format: qualifier?

Principle: "?": if the previous finite qualifier, the smallest data will be used. For example, "*" will take 0, and "+" will take 1, and if it is {3re5}, it will take 3.

Take a look at the following two codes:

Code 1.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report