gpt4 book ai didi

Php, 正则表达式

转载 作者:可可西里 更新时间:2023-10-31 22:55:45 27 4
gpt4 key购买 nike

我得到了这个模式(我正在使用 php):

'/\[link\=((https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?)\]/i'

当我搜索这个字符串时:http://phpquest.zapto.org/users/register.php

匹配项为(顺序为0-5):

  1. '[link=http://phpquest.zapto.org/users/register.php]'
  2. 'http://phpquest.zapto.org/users/register.php'
  3. 'http://'
  4. 'phpquest.zapto'
  5. org
  6. ''

当我在最后一个子模式中用 + 替换 * 时:

'/\[link\=((https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]+)*\/?)\]/i'

匹配项为(顺序为0-5):

  1. '[link=http://phpquest.zapto.org/users/register.php]'
  2. 'http://phpquest.zapto.org/users/register.php'
  3. 'http://'
  4. 'phpquest.zapto'
  5. org
  6. '/users/register.php'

如果有人能帮助我理解为什么,我将非常感激,谢谢大家,祝您有愉快的一天。

最佳答案

也许一个更简单的例子是当你比较 thisthis .

涉及的正则表达式有:

(a*)*

(a+)*

测试字符串是aaaaaa

发生的事情是,在捕获主要组(在我提供的示例中,a 的系列)之后,它会尝试匹配更多,但不能。可是等等!也可以什么都不匹配,因为*表示0次或多次!

因此,在匹配所有 a 之后,它将匹配并捕获 'nothing',并且由于只存储了最后捕获的部分,因此捕获组的结果是 ''。

(a+)*中,匹配和捕获aaaaaa后,它不能再匹配或捕获任何东西(+阻止它匹配任何东西,而不是 *),因此,aaaaaa 是最后一场比赛。

关于Php, 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18085386/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com