我正在尝试从字符串中提取两个字符串。字符串的格式如下:
$text = 'First string here(second here)';
秒字符串将始终位于引号内的末尾。我试图有效地提取它们。我试过用这个:preg_match('#\((.*?)\)#', $text, $match)
和preg_match('/\(([^\)]+)\)/', $text, $match)
上面的表达式工作正常,但我试图一次性完成,而不是分开。我想这是我的OCD踢:/
您可以使用捕获组:
$text = 'First string here(second here)';
if (preg_match('#^([^\(]+)\(([^\)]+)\)$#', $text, $match)) {
print "$match[1]\n";
print "$match[2]";
}
$out= preg_split('/[\(\)]/',$text)[1];