简体   繁体   English

PHP Regex模式混乱

[英]PHP Regex pattern confusion

I couldn't make few regex patterns to work so i googled most of them and have encountered issues in the process. 我无法使几个正则表达式模式起作用,所以我在其中搜索了大多数正则表达式模式,并在此过程中遇到了问题。 I mostly understand rules in play while building regex patterns but don't understand how to create regex that checks if in string is at least 1 certain type of character. 我在构建正则表达式模式时最了解游戏规则,但不了解如何创建用于检查字符串中是否至少包含某种特定字符的正则表达式。 This i found online: 我在网上发现的:

'/^((?=.*\\d)(?=.*[az])(?=.*[AZ])(?=.*[@#$%]).{6,20})$/'

What i cant understand is this: 我不明白的是:

  • what this is for ?=.* does it like mean something together or just separate attributes ?=.*含义是什么,意味着在一起或只是单独的属性
  • why is there . 为什么在那儿. between curly brackets and normal ones ).{ as far as i know its any character, unless it means any of previously mentioned but what makes it to work that way 在花括号和普通花括号之间).{据我所知,它的任何特性,除非它表示先前提到的任何含义,但是什么使其能够以这种方式工作
  • the last problem is that regex works the way that it actually checks if demanded characters are provided but also allow to type something like ąó or "(^! when i look at this i don't see anywhere that mentioned characters are allowed 最后一个问题是,正则表达式的工作方式实际上是检查是否提供了所需的字符,但也允许键入类似ąó"(^! ąó ,当我看到此ąó时,我看不到任何提到的字符都允许

As you already figured it out im trying to build password validation regex and struggling with this. 正如您已经知道的那样,我试图构建密码验证正则表达式并为此而苦苦挣扎。

EDIT 编辑

If its possible could someone present alteration to this regex so it will not mean 如果可能的话,有人可以对此正则表达式进行修改,所以这并不意味着

  • require one of digit, lower case, upper case, special char of provided list and than whatever else to fill blanks 需要数字,小写字母,大写字母,提供的列表的特殊字符之一,并且比其他任何字符都要填空

so it would instead mean 所以它的意思是

  • require one of digit, lower case, upper case, special char of provided list and fill blanks with characters of mentioned rules, so you can continue typing digits letters and special chars from provided list but not some special things like !óż 要求提供的列表中的数字,小写,大写,特殊字符之一,并用提及的规则字符填充空格,因此您可以继续输入提供的列表中的数字字母和特殊字符,但不能输入某些特殊字符,如!óż

(?=...) is a positive lookahead assertion . (?=...)是一个积极的超前断言 It asserts that the enclosed regex could match at the current position without actually performing the match. 它断言,封闭的正则表达式可以在当前位置匹配,而无需实际执行匹配。 So for example, (?=.*\\d) means "check to see if it's possible to match any number of characters, followed by a digit" which translates to "check if there is at least one digit somewhere ahead in the string". 因此,例如, (?=.*\\d)意思是“检查是否可以匹配任意数量的字符,后跟一个数字”,翻译为“检查字符串前面是否至少有一个数字” 。

Breaking the regex down, this means: 分解正则表达式意味着:

^             # Start of string
(             # Match and capture in group 1:
 (?=.*\d)     # Assert that there is at least one digit in the string
 (?=.*[a-z])  # Assert that there is at least one lowercase letter in the string
 (?=.*[A-Z])  # Assert that there is at least one uppercase letter in the string
 (?=.*[@#$%]) # Assert that there is at least one of the characters @#$%
 .{6,20}      # Match 6-20 characters (any character except newlines)
)             # End of group
$             # End of string

The capturing group is wholly unnecessary, by the way. 顺便说一句,捕获组完全没有必要。

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

^ assert position at start of the string ^在字符串开头的断言位置

1st Capturing group ((?=.*\\d)(?=.*[az])(?=.*[AZ])(?=.*[@#$%]).{6,20}) 第一捕获组((?=.*\\d)(?=.*[az])(?=.*[AZ])(?=.*[@#$%]).{6,20})

(?=.*\\d) Positive Lookahead - Assert that the regex below can be matched (?=.*\\d) 正向超前-断言可以匹配以下正则表达式

Any amount of any characters, followed by a digit. 任意数量的字符,后跟一个数字。 In other words, string must contain a digit. 换句话说,字符串必须包含一个数字。

(?=.*[az]) Positive Lookahead - Assert that the regex below can be matched (?=.*[az]) 正向超前-声明可以匹配以下正则表达式

Any amount of any characters, followed by a single character in the range between a and z (case sensitive). 任意数量的任何字符,后跟单个字符,介于a和z之间(区分大小写)。 In other words, string must contain a lowercase character. 换句话说,字符串必须包含小写字符。

(?=.*[AZ]) Positive Lookahead - Assert that the regex below can be matched (?=.*[AZ]) 正向超前-断言可以匹配以下正则表达式

Any amount of any characters, followed by a single character in the range between A and Z (case sensitive). 任意数量的任何字符,后跟单个字符,介于A和Z之间(区分大小写)。 In other words, string must contain an uppercase character. 换句话说,字符串必须包含一个大写字符。

(?=.*[@#$%]) Positive Lookahead - Assert that the regex below can be matched (?=.*[@#$%]) 正向超前-断言可以匹配以下正则表达式

Any amount of any characters, followed by a single character in the list @#$% literally. 任意数量的任何字符,然后按字面意义在列表@#$%中紧跟一个字符。 In other words, string must contain @#$%. 换句话说,字符串必须包含@#$%。

.{6,20} matches any character (except newline) between 6 and 20 times. .{6,20}与6到20次之间的任何字符(换行符除外)匹配。

$ assert position at end of the string $在字符串末尾的断言位置


Referenced by: 引用人:

regex101 regex101


EDIT: Change . 编辑:更改. to [^__blacklisted_characters_here__] [^__blacklisted_characters_here__]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM