In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you how to get started with the regular expression Regex, the content is very detailed, interested friends can refer to, hope to be helpful to you.
What I'm going to share today is the regular expression Regex.
One day at noon, I was about to pick up my cell phone and call Arena of Valor when suddenly there was a message from Wechat!
A netizen called and asked me if I could help to see a string matching problem.
I brazenly agreed, but when I saw the title.
The topics that need to be matched and retrieved are:
= A12/B14/D14..../H18
= C12 * D12 * H16...*F19
Well, I opened the regular manual that I hadn't touched in years.
Preliminary study on Regex
First of all, let's give a brief introduction:
A regular expression (also known as regex or regexp) is just a pattern that can be used to match different and usually specific combinations of characters.
We can use these patterns to check and process strings, how to retrieve them, we need to know how to construct them, and creating regular expressions might look like this:
Let dog; rat = / dog/; rat = new RegExp ("dog")
With expressions, how do we match? of course, regular expressions have their own methods available, and they can also be used in some string methods.
For example, .test is a method that can be used for regular expressions. It returns whether the Boolean value of the regular expression is found in the string:
Let rat = / dog/; rat.test ('I saw a dogstore'); / / returns true / rat/.test ('I saw a dogboat'); / / returns true
The above two ways of calling .test are equivalent: that is, you can call it using the variable assigned to regexp or directly using regexp. This is true for any use of regular expressions. It is important to note that regular expression matching must be accurate, including any newline characters, uppercase letters, and whitespace. For example:
/ dog/.test ('I saw some dogsgiving'); / / returns true / dog/.test ('Meet at the bad og 9'); / / returns false / dog/.test (' Rats and doihs are not friends'); / / returns false
See here, certainly do not understand, why to use it, do not rush, look down.
Special character
Special characters are characters used to modify or specify a combination of characters for regular expressions. One of the most useful special characters is square brackets. Square brackets allow you to indicate that the characters in the target string can be any number of characters! Let's see what they do:
Const bt = / b [aeiou] t but'; bt.test ('bat'); / / returns true bt.test (' bet'); / / returns true bt.test ('bit'); / / returns true bt.test (' bot'); / / returns true bt.test ('but'); / / returns true bt.test (' bpt'); / / returns false
Come to think of it, everything in parentheses corresponds to a character in the string you want to search. On top of this useful ability, we can use the "-" character to specify a specific range of characters!
Const nums = / [0-5] /; nums.test ('0'); / / returns true nums.test ('3'); / / returns true nums.test ('7'); / / returns false
And, for example, to specify all the letters, you would do something like this:
Const letters = / [A-Za-z] /; letters.test ('M'); / / returns true letters.test ('y'); / / returns true letters.test ('5'); / / returns false
Another special character to remember is the "+" character. This indicates that a particular element can be repeated any number of times. Let's see how it works.
Const bomb = / boo+m/; bomb.test ('boomboxes'); / / returns true bomb.test ('boomers'); / / returns false bomb.test ('booooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
If you want to ignore case, you can add an I after it.
Const bomb = / boo+m/i; bomb.test ('boomholes'); / / returns true bomb.test ('boomstocks'); / / returns true bomb.test ('booooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
For example, our common "?" Character is also a useful special character. This character indicates that the preceding characters may or may not be included.
Const color = / colou?r/; color.test ('color'); / / returns true color.test (' colour'); / / returns true
The special character you may also need to pay attention to is "." character. It's a wildcard. A "." can represent any other character, not including line breaks.
Const anything = /. /; anything.test ('a'); / / returns true anything.test ('1'); / / returns true anything.test ('['); / / returns true
Well, let's first mention these basics, and then let's take a look at other grammars.
Other grammars
The\ w "character refers to any alphanumeric character. Its antonym,"\ W ", refers to any non-alphanumeric character.
Const alphaNumber = /\ w const alphaNumber; alphaNumber.test ('a'); / / returns true alphaNumber.test ('1'); / / returns true alphaNumber.test ('&'); / / returns false const notAlphaNumber = /\ W Weibo; notAlphaNumber.test ('a'); / / returns false notAlphaNumber.test ('1'); / / returns false notAlphaNumber.test ('&'); / / returns true
Again, the "\ s" character refers to any white space character, while the "\ S" character refers to any non-white space character.
Const whitespace = /\ s const whitespace; whitespace.test ('a'); / / returns false whitespace.test ('1'); / / returns false whitespace.test ('&'); / / returns false whitespace.test (''); / / returns true whitespace.test ('\ n'); / / returns true const notWhitespace = /\ SAccord; notWhitespace.test ('a'); / / returns true notWhitespace.test ('1'); / / returns true notWhitespace.test ('&') / / returns true notWhitespace.test (''); / / returns false notWhitespace.test ('\ n'); / / returns false
Well, if you can't finish the introduction, you still have to check it for yourself. well, is there a summary picture? at this time, of course there is.
Solve a thousand sorrows in one picture
manual
This is a third-party website, it is recommended to combine this, the grammar reference on the right, is basically the above content.
Https://c.runoob.com/front-end/854
Debugging on the left, syntax reference in the middle and console on the right, isn't it beautiful.
Solve a problem
The matching format is = A12/B12/C12.../K23 or multiplication in the middle.
After thinking about it for a long time, first of all, can we split it into three parts, one end and one tail and the middle part?
At the beginning, can we match like this:
Reg = / ^ = [Amurz] +\ dcolor _ I
The first is the = beginning, then the letter, then the number, considering that it may be one or more, you have to use the "+" special character.
Then let's look at expectations:
Reg.test ('= b12') / / true reg.test ('= C12') / / true reg.test ('= CC3') / / true reg.test ('= CDdd35') / / true reg.test ('= CDdd35') / / false
Well, let's see how to write the tail part:
/ / tail part, it should all be / CC12, / B234, so that the subdrops reg = / (\ / |\ *) [Amurz] +\ dflowers I
First of all, considering that it may be multiplication and division, there is a * * (/ | *) * *, which needs to be escaped and then ends with $.
Reg.test ('/ cc') / / false reg.test ('/ cc12') / / true
The rest is the middle part, that is, there are several structures like / B231, so:
Reg = / ((\ / |\ *) [Amurz] +\ dflowers $) * / I
Finally, put it together, and this is what it looks like:
Const multiplicationAndDivisionReg = / ^ = [Amurz] +\ d + ((\ / |\ *) [Amurz] +\ dfuro$) * (\ / |\ *) [Amelazi] +\ dcopyright Uniplazi
After a simple introduction, leave a thinking question. I saw an interesting question in the interview before, the number in the quantile.
'10000000000.00'
/ / output '100000000.00'
Let's hurry up and have a real fight.
On how to get started on the regular expression Regex to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.