In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the skills of regular shorthand, which the editor thinks is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
How to pick out the string you want? You can read it from left to right, top to bottom, one character at a time, and write a lot of if to judge. This is too troublesome. The rule is to abstract the above process and use some specific symbols to express most of the rules.
For most people, regularities are mainly difficult to remember. In fact, as long as you classify regular symbols, you will find that the core concepts are actually very simple.
Note: this article will assume that you have read the regular document at least once, if not, you can read mdn (with Chinese translation), if you think mdn typesetting is messy, it is recommended to go to https://devdocs.io/javascript/global_objects/regexp to learn. The language used in this article is javascript
Metacharacter
The an in / a / is a metacharacter, and a metacharacter matches an entity character. The "a" here has no special meaning, so it matches an English a.
Like /\ d / matches all the numbers from 0 to 9, and if two consecutive letters /\ d\ d / can match all the numbers from 10 to 99
/. / can match any character
Set
If there is a string of '0123456789', I just want to match the'3','3','6', and 9', which can be put in the collection / [369] /.
After execution, you will find that only 3 is matched, because no matter how much is written in the set, it represents a metacharacter, and the set is also a metacharacter.
You may have seen this collection / [0-9aMuszAmurZZ] / it represents matching all numbers, letters and underscores. It has the same effect as metacharacter /\ w /, and the set is more flexible and more convenient.
Exclusive set
/ [^ 0-9] / all but the numbers match (I like to call it a negative set)
Or (multiple selection)
Suppose I have a lot of book lists and I want to match naroto and one piece, so I can write / naroto | one pice/ vertical bar represents or, an or b, or you can write more than one a | b | c | d, of course you can write 0 | 1 | 2. | 9 to match all the numbers. However, pay attention to the difference between the collection and the collection. The collection is a metacharacter and cannot match words. | there can be multiple metacharacters on both sides.
Grouping
I have a bunch of files now, and I want to match files with suffixes .css and .less, and you can write code like / .css | .less / naturally. In fact, there is a simple way to write /. (C | le) ss/, grouping can put several metacharacters in the same scope for processing, through grouping we can write more concise code
There is also a special use of grouping to think about how to match strings like 'asd_asd_asd_asd_asd''.
The answer is / (asd) (_\ 1) + /,\ 1 is a very special metacharacter, which represents the reuse of the matching result of the first group,\ 2 represents the second, and so on, the + sign indicates repetition of one or more times (as we will see later), it is important to note that the count starts at 1, and\ 0 represents another meaning (see documentation)
Quantifier
I only want to match consecutive numbers in a long string of characters, but /\ d / will only match one, so you can use the quantifier /\ d {n,} /, n to write as many as possible, for example, {1,} is 1 to more. {n} is written as n connected, and matching 2333 can be written as / 23 {3} /.
There are also several abbreviated quantifiers.
+ represents 1 to more, equal to {1,}
* stands for 0 to more, equal to {0,}
? Stands for 0 or 1, which is equal to {0pm 1}.
Greedy mode and non-greedy mode
The quantifier has an awkward place, such as using /. * a / to match '123a 123a'. It was supposed to get '123a' but actually got '123a123a'. This is because any character satisfies /. / plus a quantifier will cause the match from beginning to end, but because we have other metacharacters, the regular engine will backtrack and take out the matching results one by one from back to front to match the remaining metacharacters.
This mode is called greedy mode, and it may produce unexpected results and unnecessary waste of performance.
The solution is to use non-greedy mode and add after the quantifier. The question mark can get the minimum result, now you can get '123a' by using /. *? a / to match. Non-greedy mode can be used after any quantifier
Look around
X (? = y) this function has many kinds of translation, such as zero width assertion. I personally feel that what is more accurate is "positive positive look around".
X (?! y) positive negative look around
X stands for metacharacters, and y also stands for metacharacters. X (? = y) means that the x of y is immediately followed by the x of y, such as'- 1a. If you use /\ d (? = a) / to match, you will get 1 /\ d (?! a) / to match, you will get 2.
How do I use this function? For example, there is a string'a (123) bounded. I only want the contents in parentheses, but I don't want parentheses.
I need to match to the left of the closing parenthesis, so I can write / (? =\) / (note that the parentheses need to be escaped). I don't want the left parenthesis / [^ () /, I don't care about the contents of the parentheses /. * /, then combining the three regularities becomes / [^ (]. * (? =\)) /
In fact, this function matches the position, looking for metacharacters from the matching position, so it's useless if you add quantifiers after looking around.
Other
^ and $are also metacharacters of the matching position, which are the beginning and end of the match, respectively. For example, if we want to match a file that ends with .js, it can be written as / .js $/. Links that match the beginning of http can be written as / ^ http:\ /\ / /
There are also some special\ u [\ b]\ 0 and so on, you need to read the documents for yourself.
Identifier
G: a regular will only match once. If you add the g identifier, it will match globally. /\ dUnig. This regular means that all numbers will be matched no matter what is separated between the two numbers.
I: case-insensitive / ^ http:\ /\ / / I will match http:// and HTTP://
That's all the core concepts are. Please check the documentation for more information.
You think it's over? In fact, there is a sequel!
I'm going to go on and look around.
There is also a miraculous reverse look without saying x (?
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.