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>
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>
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>
Change:
<code>preg_match('/^(?:(?:\w+\.?\w+)-?){4}$/', 'lo-n.l-on.loh-4va.ccccc5om'); </code>
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>
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>
After:
Only 34 steps required.
Analyze the matching process.
The subject of the question:
Modified matching steps:
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.
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.