开发者

Can someone please help me understand this Regular Expression?

开发者 https://www.devze.com 2022-12-18 18:44 出处:网络
I have the following regular expression: (?=.{8,})[a-zA-Z]+[^a-zA-Z]+|[^a-zA-Z]+[a-zA-Z]+ I do not understand the \"?=\" part of it.

I have the following regular expression:

(?=.{8,})[a-zA-Z]+[^a-zA-Z]+|[^a-zA-Z]+[a-zA-Z]+

I do not understand the "?=" part of it.

My basic (incorrect) understanding is that it is saying a string that is 8 characters or more long, which has one or more letters followed by one or more non-letters, or one or more non-letters followed by one or more letters. 开发者_如何学运维 My understanding is obviously not correct.

The engine is .NET.

Any help would be appreciated.


It looks like a simple password minimum strength verifier. It matches anything that is at least 8 characters long and contains at least one letter and one non-letter (in any order).

The (?=..) is a lookahead which must match, but doesn't consume any characters. If there are fewer than 8 characters, the lookahead fails, so the entire match fails. If the lookahead succeeds, the rest of the regex must still match, but it starts checking from the beginning because no characters have been consumed yet.

If you wrote it without the lookahead, the term .{8,} would consume all of the characters in the string, so there would be nothing left for the rest of the expression to match, so it would always fail.

An alternative way to write this expression would be:

^(?=.{8})(?=.*?[a-zA-Z])(?=.*?[^a-zA-Z])

This uses only lookaheads, but the meaning is roughly the same.

I also added an anchor ^ at the beginning to avoid unnecessary extra searching if the match fails.


The ?= part starts a positive lookahead, meaning what else is in the parentheses is required to appear here, but it doesn't consume any characters in the match.

Basically, the part (?=.{8,}) requires that the string is at least 8 characters long, as you already noted.

After that you have an alternation which matches at least one ASCII Latin letter followed by at least one non-letter or at least one non-letter followed by at least one ASCII Latin letter.

I'm not too sure about the alternation, it might well be that the lookahead at the start, requiring the string to be at least 8 characters long, will only be required if the string starts with a letter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号