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
?
?