Common mistakes for newbies learning regular expressions
The advantage of regular rules is that they are easy to use. After a few hours of study, you can understand most of the regular rules. Although you can understand them, in the process of practice, you will still encounter many things that you don’t want to know. If you want results, after all, the grammar of regular expressions is still a bit strange. This article has compiled some mistakes that are often made in the learning process of regular expressions.
Regular expression
1. Space
We usually write code At times, spaces are usually used as a tool to make the code more standardized. Together with appropriate indentation and tabs at the beginning of the line, the code looks clearer. But you have to be careful in regular expressions - the space itself is also a character to be matched. If you use spaces inappropriately:
echo preg_match('/a{1, 3}/', "aaa") ? '匹配' : '不匹配'; // 不匹配
For example, the above regular expression is intended to match 1 to 3 a's, but in fact will not match the following three a's, because there is an extra space in the middle of {1, 3}, which invalidates the original meaning of the metacharacter "{}" and becomes an ordinary character:
echo preg_match('/a{1, 3}/', "a{1, 3}") ? '匹配' : '不匹配'; // 匹配
"a {1, 3}" is matched instead, which is obviously not what we want, so be sure to note that unless the space character itself is matched, do not use spaces:
echo preg_match('/a{1,3}/', "aaa") ? '匹配' : '不匹配'; // 匹配
※ Exceptions The pattern modifier It is difficult to understand, and it is not recommended to use:
echo preg_match('/a a a/x', "aaa") ? '匹配' : '不匹配'; // 匹配
2. Capitalization
This is easy to understand, but it is basically a careless mistake. After all, we usually use When searching for letters in a search tool, both uppercase and lowercase letters are usually matched. Sometimes, you forget that regular expressions do not automatically match uppercase and lowercase letters:
echo preg_match('/flag/', "Flag") ? '匹配' : '不匹配'; // 不匹配
There may be cases where the first letter of the string matched like this is capitalized. Naturally, there will be no match. At this time, we must take into account both upper and lower case. But sometimes we want to match a certain word. As long as these four letters are matched together, it is more troublesome to write:
echo preg_match('/[Ff][Ll][Aa][Gg]/', "Flag") ? '匹配' : '不匹配'; // 匹配
Although it is difficult to imagine that there is such a weird thing as "fLaG" Writing method, but if you don’t write it like this, you can’t match all situations, but sometimes we don’t care about case, but the string to be matched is very long. I’m afraid I’ll be exhausted if I write it like this, but fortunately we have the “i” modification. Symbol:
echo preg_match('/flag/i', "Flag") ? '匹配' : '不匹配'; // 匹配
When the modifier "i" is set, the case matching in the pattern will be insensitive.
3. Greedy mode
Quantifiers " " and "*" are greedy mode by default. Beginners may not encounter the problems it brings. I don’t understand what this means. Here’s an example from kano:
preg_match_all('/<span>.*<\/span>/', "<span>aaa</span><span>bbb</span>", $matches); var_dump($matches);
The original intention of the regular expression is to find all the span tags in the string. , and put them into an array, but the result is strange: both spans were matched at once! In fact, this is reasonable if you think about it. The string "aaabbb" does indeed start with and end with , but the .* in it matches too many contents, "aaabbb" are all matched. This is the greedy mode of " " and "*" - by default they will match as many characters as possible, and adding a "?" at the end can cancel this greedy mode, allowing them to match only as little content as possible:
preg_match_all('/<span>.*?<\/span>/', "<span>aaa</span><span>bbb</span>", $matches); var_dump($matches);
This time we got the results we wanted.
The regular syntax is quite special, and it’s easy to get into trouble if you don’t pay attention.
Recommended study: "Quick introduction to regular expressions"
The above is the detailed content of Common mistakes for newbies learning regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

PHP is a programming language widely used in Web development. It can interact with various databases to implement data query and operation. When using PHP for query operations, we need to pay special attention to some matters to avoid common errors. The following will introduce the precautions and common errors of PHP query statements in detail, and provide specific code examples to help readers better understand. 1. Connect to the database Before performing PHP query, you first need to connect to the database. Use PHP's built-in mysqli (MySQLI

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL

Sharing tips on using PHP regular expressions to implement the Chinese replacement function. In web development, we often encounter situations where Chinese content needs to be replaced. As a popular server-side scripting language, PHP provides powerful regular expression functions, which can easily realize Chinese replacement. This article will share some techniques for using regular expressions to implement Chinese substitution in PHP, and provide specific code examples. 1. Use the preg_replace function to implement Chinese replacement. The preg_replace function in PHP can be used

"Full Analysis of Common WordPress Mistakes: Tips to Save Your Website" As a popular website construction platform, WordPress will inevitably encounter some problems and errors during use. This article will provide a detailed analysis of some common WordPress errors and provide specific code examples to help website administrators easily solve problems and improve website operation efficiency and user experience. 1. White screen error When a white screen appears when visiting a WordPress website, it is usually caused by a PHP code error.