此正则表达式中的前瞻说明 [英] Explanation of Lookaheads in This Regular Expression

查看:55
本文介绍了此正则表达式中的前瞻说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当了解正则表达式,但我不能经常使用它们来成为专家.我遇到了一个用于验证密码强度的正则表达式,但它包含一些我不熟悉的正则表达式概念.正则表达式是:

I understand regular expressions reasonably well, but I don't get to make use of them often enough to be an expert. I ran across a regular expression that I am using to validate password strength, but it contains some regex concepts I am unfamiliar with. The regex is:

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$

用通俗的英语来说,这意味着该字符串必须至少包含一个小写字符、一个大写字符和一个数字,并且该字符串必须至少有六个字符长.谁能为我分解一下来解释这种模式实际上是如何描述该规则的?我看到字符串 char ^ 的开头和字符串 char $ 的结尾,三个带有前瞻的组,匹配任何字符.和重复 {6,}.

and in plain English it means that the string must contain at least one lowercase character, one uppercase character, and one number, and the string must be at least six characters long. Can anyone break this down for me to explain how this pattern actually describes that rule? I see a start of string char ^ and an end of string char $, three groups with lookaheads, a match any character . and a repetition {6,}.

感谢任何可以帮助我解决这个问题的正则表达式专家.

Thanks to any regex guru who can help me get my head around this.

推荐答案

正常情况下,一段正则表达式匹配一段输入字符串,并消耗"那段字符串.表达式的下一段匹配字符串的下一段,依此类推.

Under normal circumstances, a piece of a regular expression matches a piece of the input string, and "consumes" that piece of the string. The next piece of the expression matches the next piece of the string, and so on.

前瞻断言消耗任何字符串,因此您的三个前瞻断言:

Lookahead assertions don't consume any of the string, so your three lookahead assertions:

  • (?=.*\d)
  • (?=.*[a-z])
  • (?=.*[A-Z])

每个都表示此模式(后跟数字、小写字母、大写字母的任何内容)必须出现在字符串中的某处",但它们不会将当前匹配位置向前移动,因此其余的表达:

each mean "This pattern (anything followed by a digit, a lowercase letter, an uppercase letter, respectively) must appear somewhere in the string", but they don't move the current match position forwards, so the remainder of the expression:

  • .{6,}

(表示六个或更多字符")仍必须匹配整个输入字符串.

(which means "six or more characters") must still match the whole of the input string.

这篇关于此正则表达式中的前瞻说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆