简体   繁体   English

使用“{}”和“?”的Preg_match()用法

[英]Preg_match() usage with “{}” and “?”

I'm currently trying things with reggae (PCRE) and PhP. 我目前正在尝试使用雷鬼(PCRE)和PhP。

It seems that the function pre_match() does not work properly with the signs {} and ? 似乎函数pre_match()无法使用符号{}和? and I'm wondering why. 而且我想知道为什么。 Can someone help me? 有人能帮我吗?

Here is the code I'm using just for the example: 以下是我仅用于示例的代码:

    <?php
        $search = "aaaah";

        //Return "valid" while I ask for 3 "a" specifically
        if(preg_match("&a{3}&", $search)){
              echo 'Valid <br/>';
        }else{
              echo 'Invalid <br/>';
        }

        //Also return "valid" while I ask for 0 or 1 "a"
        if(preg_match("&a?&", $search)){
              echo 'Valid <br/>';
        }else{
              echo 'Invalid <br/>';
        }
    ?>

There must be something I misunderstood. 必须有一些我误解的东西。 Can someone explain? 谁能解释一下? I did not find anything anywhere. 我什么都没找到。 Thank you in advance! 先感谢您!

I'm using MAMP with a Mac and coding in utf-8. 我在Mac上使用MAMP并在utf-8中编码。

The results you get are expected as: 您获得的结果预计为:

  1. You ask for 3 a 's in a row and in your example there are two of those series possible, the first 3 a 's and the last 3 a 's. 你要求连续3 a ,在你的例子中有两个系列可能,前三个a和后三个a So that is a match as you don't specify what has to happen before or after your series of 3 a 's. 所以这是一个比赛,你没有指定什么有之前或您的系列的第3之后发生的a的。
  2. You ask for an optional a character. 你要求一个可选a角色。 Any input will match that as you are looking for 0 or 1 a's and that will always be true for everything you feed it, whether it contains a 's or not. 任何输入将匹配你正在寻找的0或1的,这将永远是你给它的一切属实,是否包含a的或没有。

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

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