我需要重构一些PHP注释,我想将string[]
替换为array<int, string>
。
我尝试使用此PCRE正则表达式匹配适当的注释:
(?<!\$|>)\w+\[\]
但是它不起作用,here's how the regex is matching:
最新的两行不应匹配。有什么方法可以为此创建工作的正则表达式,还是应该使用创建自定义脚本来做到这一点?
您可以使用
(?<!\$|->)\b\w+\[]
请参见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);
}