Why does this PHP regular expression preg_match sometimes return false?

WBOY
Release: 2016-09-19 09:16:24
Original
1156 people have browsed it

As in the title:

<code class="php">echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.ccccc5om'));//return 0
echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.cccccom'));//return 1
echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccc5com'));//return false
echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccccom'));//return 1</code>
Copy after login
Copy after login

Reply content:

As in the title:

<code class="php">echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.ccccc5om'));//return 0
echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.cccccom'));//return 1
echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccc5com'));//return false
echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccccom'));//return 1</code>
Copy after login
Copy after login

Look at the code you are just filtering. int(0) means there is no match, int(1) means the match is successful.

Let me help you change the code. Group capture cannot be used indiscriminately, regular writing must be concise, and operating efficiency must be improved as much as possible, and unnecessary variables must not be stored. You need to understand the backtracking principle of the regular engine to understand what you are doing.

For more basic knowledge, you must at least understand the difference in matching principles and operating efficiency between greedy matching and non-greedy matching.

Original:

<code>preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.ccccc5om');</code>
Copy after login

Change:

<code>preg_match('/^(?:(?:\w+\.?\w+)-?){4}$/', 'lo-n.l-on.loh-4va.ccccc5om'); </code>
Copy after login

I will analyze it based on the question in question.

Take the first piece of code from the question as an example:

<code>preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.ccccc5om')</code>
Copy after login

Why does this PHP regular expression preg_match sometimes return false?

It took a total of 74781 steps, more than 70,000 steps!
Changed to the one I modified above

<code>preg_match('/^(?:(?:\w+\.?\w+)-?){4}$/', 'lo-n.l-on.loh-4va.ccccc5om');</code>
Copy after login

After:

Why does this PHP regular expression preg_match sometimes return false?

Only 34 steps required.

Analyze the matching process.
The subject of the question:

Why does this PHP regular expression preg_match sometimes return false?

Modified matching steps:

Why does this PHP regular expression preg_match sometimes return false?

74781 steps: 34 steps is about 2200%, the efficiency is really different.

----Supplemented in dec 29 ---
About regular learning:
I studied it in detail in learning perl. Personally, I think the following points need to be followed to use regular rules well:
1 Distinguish when to use greedy matching and non-greedy matching
2 Use anchors as much as possible
3 Group what can be grouped as much as possible
4 Instead of enumeration, it is better to reverse filter and sum metacharacters To summarize, for example, [a-zA-Z0-9]+ is not as good as w+. In some specific scenarios, w+ is not as good as 1+. If you add anchor characters (b anchor words, ?= ?>, ?! Forward and reverse pre-search), it will be faster. The so-called quantifier nesting means that greedy matching should not be used (use it as little as possible when using +*, which can greatly reduce backtrack, that is, the number of backtracking.
More In terms of perceptual understanding, I want to find the male among 1 billion people lined up in a row. Just tell me the most obvious one and a few characteristics. For example, those two trees in a row that are 1 million square kilometers apart are all You are a male, there is no need to tell me 100 characteristics and let me check them one by one

.

Above.


  1. -s ↩

http://php.net/manual/zh/func...

preg_match() returns the number of matches for pattern. Its value will be 0 (no match) or 1 because preg_match() will stop searching after the first match. preg_match_all() differs from this in that it searches for the subject until it reaches the end. If an error occurs preg_match() returns FALSE.

Related labels:
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