In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the regular how to achieve solidification grouping to improve efficiency, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Specifically, use "(? >...) "is no different from a normal match, but if the match occurs after this structure (that is, after closing parentheses), then all standby states in this structure are discarded (cannot be traced back).
In other words, at the end of the solidified packet matching, the text it has already matched has been solidified into a unit and can only be retained or discarded as a whole. The untried standby states in the subexpressions in parentheses no longer exist, so backtracking can never select the state (at least, the state in which "locked in" is in when the structure match is complete).
Example:
For example, to deal with a batch of data, the original format is 123.456, but later, due to the problem of floating point display, part of the data format is changed to 123.456000000789, which requires that only 2-3 digits after the decimal point should be retained, but the last digit cannot be 0. How to write this rule? (let's directly consider the number after the decimal point.) after writing the rule, we also need to use this regular to match the data and replace the original data with the matching result.
Rule one,
The code is as follows:
$str = preg_replace ('\. (\ d\ d [1-9]?).
/ / the group1 of the matching result is backreferenced
Obviously, this way of writing, for part of the data format is 123.456 of this format, in vain to deal with, in order to improve efficiency, we also have to deal with this regularization. Comparing the string 123.456 with others, we find that there is no number behind the data of 123.456, so we deal with it for nothing. That's easy. Let's modify the regularization and change the quantifier * to + so that it will not be in vain for those with two digits after the 123.45 decimal point, and for those with more than three digits, it will be handled normally. Its PHP code is
Regular II.
The code is as follows:
$str = preg_replace ('\. (\ d\ d [1-9]?).
All right, is there really no problem with this regularity? Next, let's also analyze this regular matching process.
The string "123.456" and the regular expression is [\. (\ d\ d [1-9]?)\ d +], let's take a look.
First of all (stop talking about 123 before the decimal point)
[\.] match ".", match successfully, give control to the next [\ d], [\ d] match "4" successfully, give control to the second [\ d], this [\ d] matches "5" successfully, and then give control to [[1-9]?], because the quantifier is [?], the regular expression follows the "quantifier first match", and, in this case, [?], there is a retrospective point. Then match "6" successfully, then give control to [\ d +], [\ d +] find that there are no characters behind, most follow the "last in, first out" rule, go back to the last retrospective point, match, at this time, [[1-9]?] will return the matching character "6", [[1-9]?] match "6" successfully. The match is complete. We found that the result of [(\ d\ d] [1-9])] match is indeed "45", not the "456" we want. "6" is matched by [\ d +]. So, what should we do? Can you let [1-9]?] once the match is successful, there is no backtracking? This uses what we call "solidified grouping" above. The regular engine used in PHP (preg_replace function) supports solidified grouping. Depending on how the solidified grouping is written, we can change the code to the following way.
Regular III.
The code is as follows:
$str = preg_replace ('\. (\ d\ d (? > [1-9]?)).
If changed to this way, the string "123.456" does not meet the requirements and will not be matched. Then we can fulfill our demands.
So, let's look at (\. D\ d (? > [1-9]?))\ d +.
In the solidified group, the quantifier works properly, so if [1-9] does not match, the regular expression will return? The standby state left behind. Then match away from the curing group and proceed to "\ d +". In this case, when control leaves the solidified group, no standby state needs to be abandoned (because no standby state is created in the solidified group).
If [1-9] can match, the saved standby state still exists after the match is separated from the solidified packet. However, because it belongs to the end of the solidified group, it will be discarded.
This happens when matching'. 625'or'. 625000'. In the latter case, abandoning those states will not cause any trouble, because "\ d +" matches'. 625000, where the regular expression has completed the match. But for'. 625', because the "\ d +" does not match, the regular engine needs backtracking, but the backtracking cannot be done because the standby state no longer exists. Since there is no standby state that can be traced back, the overall match fails,'. 625 'does not need to be processed, which is exactly what we expect.
Thank you for reading this article carefully. I hope the article "how to achieve regular grouping and improve efficiency" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 287
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.