By Hao Lee [email protected]
Abstract
In fact, regular is not a particularly technical knowledge system. Its function is nothing more than the following three points: analyzing whether the string satisfies a certain pattern test, replacing the specified substring REPLACE, and capturing one or more target substring exec. This article will help you solidify some of the less friendly uses of re based on your prior knowledge of it.
Capture and non-capture ()(? 🙂
A catch is a character that you can catch with exec, and a non-catch is a character that you can’t catch with exec.
Cases of the 2019-11-1; 20190112; 2017 2 8;
Write a re that captures all the years, months and days in the example above.
(\d{4})[-\s]? (\ d {1, 2}) [\ s]? (\ d {1, 2}))
Put the substring you want to match in parentheses, and give the rest to the re, which will place the result in the array. They’ll appear at index = 1, 2, 3, respectively.
So what if I don’t want to capture the moon, which is easy, before the middle bracket? =
(\d{4})[-\s]? (? : \ d {1, 2}) [\ s]? (\ d {1, 2}))
One more question: if you write a nested re, how do you determine the rank of the re in the result array? An easy way to do this is to see how many times the left parenthesis appears. Let’s also take this example, let’s say, let’s put the whole month-day thing in little brackets.
(\d{4})[-\s]? ((\ d {1, 2}) [\ s]? (\ d {1, 2})))
What is the index of month – day? Count the number of parentheses on the left. The answer is 2.
Look around (? (=)? (< =)? !). (? The <!
This thing is also called forward check, negative check. It doesn’t matter what it’s called, what matters is whether you understand it and can use it.
- (? =) must be followed by the string of xx
- (? <=) must be preceded by the string xx
- (? !). It cannot be followed by a string of xx
- (? The
Case matching $$123; $xxxx$as; $XXX $DDDD $; $$ asafa $$; sdfs
That is, let you only match the contents sandwiched between the two single $
(? <! \ $) \ $(? ! \ $) + (? <! \ $) \ $(? ! \ $)
It looks a little complicated, but the front and the back are the same. We only see
(? <! \ $) \ $(? ! \ $)
In the middle, it means, there’s a $, and it can’t be preceded by a $, and it can’t be followed by a $. That’s what I’m trying to say.
Let’s do another example
Example match password, at least 8 characters, at least one lowercase letter, one uppercase letter, and one digit.
(? =.*[A-Z])(? =.*[a-z])(? =. * [0-9]) {8}
? = means because it must be followed by something, it must start with nothing, it must be followed by several digits (or none) and a capital letter, it must be followed by several digits and a lowercase letter, it must be followed by several digits and a number. Also, this password must be at least 8 digits. This is what regular translation means.
Cases of [email protected]; [email protected] does not match [email protected]
@ (? ! Ke) means @ cannot be followed by ke.
Back in the \ n
Backtracking means using a number to represent a previously captured element with index = n
Let’s take an example:
Example match 12321; 899812; 99099; 11233219 does not match 1234; 7395; 12902
We can see that if there are at least two consecutive palindromes we match, so we can write it this way
(\w)(\w).*(\2)(\1)
The second matching character comes first, and the first matching character comes after.
conclusion
These three places should be the most difficult part of regular, basically you can master these three technical points, regular for you there is no difficulty. But I did see an anti-human regex problem that asks you to match prime x’s. This is actually very easy to do without the regular, but it becomes more difficult to use the regular. Those who are interested can have a try. If you are willing to discuss, please contact lijiahao043 and reply from time to time.