This PHP regular expression Preg_match Why does it sometimes return false?

Source: Internet
Author: User
Tags php regular expression
Title:

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 0echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.cccccom'));//return 1echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccc5com'));//return falseecho var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccccom'));//return 1

Reply content:

Title:

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 0echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lo-n.l-on.loh-4va.cccccom'));//return 1echo var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccc5com'));//return falseecho var_dump(preg_match('/^(([a-zA-Z0-9]+\-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+$/i','lon.lon.lon.loh4va.cccccom'));//return 1

Look at the code you just filter. Int (0) represents no match to, Int (1) is a match success.

To help you change the code, packet capture can not be used, regular writing to concise, but also to maximize the efficiency of operation, but also can not store unnecessary variables. To understand the backtracking principle of the regular engine, you know what you're doing.

For a bit more elementary knowledge, you should at least understand the difference between greedy and non-greedy matching principles and operational efficiencies.

Original:

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

Change:

I'll analyze it with the question of the main problem.

Take the first paragraph of the main question code as an example:

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

Spend 74781 steps, more than 70,000 steps!
Change it to my modified

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

After:

It only takes 34 steps.

Analyze the matching process.
Title of the main:

Modified matching steps:

74781 steps: 34 Steps is approximately equal to 2,200%, the efficiency is really a difference.

----added to Dec---
About Regular Learning:
I studied it in detail in learning Perl. Individuals feel that the use of good regular is mainly to follow the following points:
1 distinguish when to use greedy match, non-greedy match
2 try to use the anchor position symbol
3 groups to be grouped as far as possible
The 4-dollar character is not as good as the inverse filter and generalization, such as, [a-za-z0-9]+ is not as good as w+, in certain scenes, w+ is inferior to 1+, if Add anchor position character (b anchor word,? Forward search), it's quicker. The so-called quantifier nesting, is not to use greedy matching (+* when the use of less, this can greatly reduce the backtrack, that is, the number of backtracking.
More perceptual understanding, I want to find out 1 billion men lined up in a row, you directly tell me the most obvious one over a few characteristics on the line, such as the two 1 million square kilometers apart in a row is male, there is no need to tell me 100 features let me check it one.

Above.

    1. -S

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

Preg_match () returns the number of occurrences of pattern. Its value will be 0 times (mismatched) or 1 times, because Preg_match () will stop the search after the first match. Preg_match_all () differs from this, and it searches subject until the end is reached. If an error occurs, Preg_match () returns FALSE.

  • 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.