PHP regular forward-looking and looking back analysis application

Source: Internet
Author: User
Tags expression engine
PHP regular forward and looking back analysis applications

Log analysis is often done with thousands of log entries, and in order to find specific patterns of data in a large amount of data, it is often necessary to write many complex regular expressions. For example, enumerating entries in a log file that do not contain a particular string, identifying entries that do not begin with a particular string, and so on.

?

There is the concept of foresight (Lookahead) and looking Back (lookbehind) in regular expressions, which describe the matching behavior of the regular engine very vividly. It is important to note that the front and back in regular expressions are a bit different from what we generally understand. A piece of text, we generally used to refer to the direction of the beginning of the text "front", the end of the text is called "back." for the regular expression engine, however, because it is parsed from the head of the text to the end (the parsing direction can be controlled by a regular option), the direction of the text tail is called "front", because this time the regular engine has not reached that block, and the direction of the text head is called "after", Because the regular engine has gone through that piece of land.

?

The so-called forward-looking is to match the regular expression to a character, the "unresolved text" in advance to see if the match/mismatch pattern, and then, in the regular engine has matched the text to see if the match/non-conforming pattern. Conforming and not conforming to a particular pattern of matching we are also known as positive and negative matches.

The modern advanced regular expression engine generally supports both the forward-looking and the looking back support is not very broad, so we use a negative forward-looking to achieve our needs.

?

The first group has nothing to match, and the second group matches the entire string. Look back and analyze the regular expression. In fact, when the regular engine resolves to zone A, it has started to perform the forward-looking work in area B. This time found that when the A zone is null, the match succeeds ――.* to match the null character, and the forward-looking conditions are met, followed by a "2009" string, not robots. So the entire matching process successfully matches to all entries.

?

Move the. * Into the forward-looking expression

?

?

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