In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to construct complex regular expressions, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
evade the crucial point
The text given by Snopo is as follows: or and name='zhangsan' and id=001 or age > 20 or area='%renmin%' and like, ask how to extract the correct SQL query statement.
A brief analysis shows that the middle part is in line with the requirements, but there are a number of like, or and and at both ends. It should be complicated to construct regular expressions that can parse query statements that conform to SQL syntax. However, for specific problems, it can also be simpler. The above poorly constructed SQL statement should be automatically generated by the program, and there will be some text at both ends that do not conform to the meaning of the topic. All you have to do is remove the text.
So I wrote the regular expression: s / ^ (: (?: or | and | like)\ s *) + |\ s * (: (?: or | and | like)\ s *) + $/ / mi;, so that the like, or, and and possible white space characters at the beginning and end of the multiline string are removed, and the rest is what is required.
divide and rule
After the answer was sent, Snopo was obviously not satisfied with this "lazy" approach. He went on to ask whether regular expressions could be written to match conditional queries that match the requirements of SQL syntax. (just consider the where section, and you don't have to write a complete select. )
Indeed, from the perspective of rapid problem-solving, any method can be used as long as it can be solved effectively, but from the perspective of learning knowledge, it is the right way not to avoid the important points but to get to the bottom of the problem. In that case, take a look at how to use the regular to solve the SQL query.
The simplest query statement should be true or false, that is, where 1; where True; where false, and so on. Such statements use regular expressions, directly / (?: -?\ d + | True | False) / I.
A slightly more complex single statement can be compared left and right, that is,
Name like 'zhang%', or age > 25, or work in (' it', 'hr',' RenewD')
. Simplify it and the structure becomes An OP B. Where A represents the variable, OP represents the comparison operator, and B represents the value.
A: the simplest A should be\ w +. Considering the actual situation, the variable contains periods or delimited characters, such as `table.salary`, which can be marked as / [\ w.`] + /. This is a more general refinement. If the requirements are more stringent, it is also possible to let the delimited characters appear on the left and right sides at the same time (condition judgment).
Several relationships commonly used in OP: Where are compared as follows: =, >, =
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: 0
*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.