渴望的量词前的负向后看

问题描述 投票:-1回答:1

我需要重构一些PHP注释,我想将string[]替换为array<int, string>

我尝试使用此PCRE正则表达式匹配适当的注释:

(?<!\$|>)\w+\[\]

但是它不起作用,here's how the regex is matching

Regex 101 preview

最新的两行不应匹配。有什么方法可以为此创建工作的正则表达式,还是应该使用创建自定义脚本来做到这一点?

php regex pcre
1个回答
0
投票

您可以使用

(?<!\$|->)\b\w+\[]

请参见PCRE regex demo

请参见PHP demo

$str = '/** @var string[] */
/** @return string[] */
* @param Company[]|null $companies

$icons[] = static::getIconDetailsFromLink($link);
$this->properties[] = $property;';

if (preg_match_all('/(?<!\$|->)\b\w+\[]/', $str, $matches)) {
  print_r($matches);
}
© www.soinside.com 2019 - 2024. All rights reserved.