Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match中的符号是什么意思?_Php_Preg Match - Fatal编程技术网

Php preg_match中的符号是什么意思?

Php preg_match中的符号是什么意思?,php,preg-match,Php,Preg Match,我在脱机借用的代码片段中有这个表达式。它强制新用户拥有一个密码,该密码不仅需要上限+下限+数字,而且必须按此顺序!如果我输入下限+上限+数字,则失败 if (preg_match("/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $pw_clean, $matches)) { <pre> <?php // Only the last 3 fail, as they should. You claim the first

我在脱机借用的代码片段中有这个表达式。它强制新用户拥有一个密码,该密码不仅需要上限+下限+数字,而且必须按此顺序!如果我输入下限+上限+数字,则失败

if (preg_match("/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $pw_clean, $matches)) {
<pre>
<?php
// Only the last 3 fail, as they should. You claim the first does not work?
$subjects = array("aaB1", "Baa1", "1Baa", "1aaB", "aa1B", "aa11", "aaBB", "aB1");

foreach($subjects as $s)
{
    $res = preg_match("/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $s, $matches);
    echo "result: ";
    print_r($res);

    echo "<br>";
    print_r($matches);
    echo "<hr>";
}
我在网上搜索过,但找不到一个能告诉我某些字符含义的资源。我可以看到模式是preg_match(“/some expression/”,yourstring,yoursmatch)

这些是什么意思:

1.  ^          -  ???
2.  .*         -  ???
3.  (?=.{4,})  -  requires 4 characters minimum
4.  (?.*[0-9]) -  requires it to have numbers
5.  (?=.*[a-z])-  requires it to have lowercase
6.  (?=.*[A-Z])-  requires it to have uppercase
7.  .*$        -  ???

如果你不知道这个网站,你应该马上去那里

这就像是正则表达式的圣经


以下是直接的答案。我把它们写得很短,因为如果不理解正则表达式,它们就没有意义。这种理解最好在regular-expressions.info中获得。我建议您也尝试一下这里列出的方法,它们允许您进行实验-在编辑模式时查看实时捕获/匹配,非常有用


1:插入符号
^
是一个锚,它意味着“干草堆/字符串/行的开始”

  • 如果插入符号是字符类
    []
    中的第一个符号,则它具有不同的含义:它否定该类。(因此在
    [^ab]
    中,插入符号使该类与任何非ab类匹配)
2:点
和星号
*
有两个不同的用途:

  • 点匹配除换行符以外的任何单个字符
    \n
  • 星号表示“允许零个或多个前述类型”
当这两个组合为
*
时,它基本上会读取“在换行符或其他规则生效之前,任何内容都不会出现”

7:dollar
$
也是一个类似插入符号的锚点,具有相反的功能:“干草堆的尽头”


编辑:

1.  ^          -  ???
2.  .*         -  ???
3.  (?=.{4,})  -  requires 4 characters minimum
4.  (?.*[0-9]) -  requires it to have numbers
5.  (?=.*[a-z])-  requires it to have lowercase
6.  (?=.*[A-Z])-  requires it to have uppercase
7.  .*$        -  ???
简单的括号
()
围绕着某个东西,使它成为一个组。这里有
(?=)
,这是一个断言,特别是一个积极的前瞻性断言。它所做的只是从当前光标在干草堆中的位置向前看,检查里面的东西是否确实存在。还有我吗?
示例:
foo(?=bar)
仅当后跟
bar
时才匹配
foo
<代码>条从不匹配,只返回
foo

考虑到这一点,让我们分析一下您的正则表达式:

/^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/

Reads as:
        ^.* From Start, capture 0-many of any character
  (?=.{4,}) if there are at least 4 of anything following this
(?=.*[0-9]) if there is: 0-many of any, ending with an integer following
(?=.*[a-z]) if there is: 0-many of any, ending with a lowercase letter following
(?=.*[A-Z]) if there is: 0-many of any, ending with an uppercase letter following
        .*$ 0-many of anything preceding the End
你说密码字符的顺序很重要——这在我的测试中并不重要。请参阅下面的测试脚本。希望这能澄清一两件事。如果您正在寻找另一个更宽容的正则表达式,请参阅



要首先使用正则表达式,您需要学习语法。这种语法由一系列字母、数字、点、连字符和特殊符号组成,我们可以使用不同的括号将它们组合在一起


看看这个链接。一种学习正则表达式的简单方法。

在谷歌上搜索与您的实际问题无关的Reg-Ex教程,但强制人们使用过于复杂的密码模式可能会导致他们将密码写在某个地方,从而消除密码的安全性。因为您正在标记此PHP:Thx ccondrup。我想我理解你的解释,但如果不是太麻烦的话,我想解释一下为什么完整表达式的工作方式会使我的密码验证变得如此复杂…很高兴听到这是可以理解的。我在我的答案中添加了更多内容,看看这个帮助是否是
preg_match(.*)
的意思?@BannedfromSO使用优秀的Regulex可视化工具来获得像这样的简单表达式的答案-(看看如果删除“?”)该页面不可用