繁体   English   中英

使用preg_match从字符串中提取两个字符串

[英]Extracting two strings from a string using preg_match

我试图从一个字符串中提取两个字符串。 字符串的格式如下:

$text = 'First string here(second here)';

秒字符串将始终在引号内的末尾。 我正在尝试有效地提取它们。 我试过使用此: preg_match('#\\((.*?)\\)#', $text, $match)preg_match('/\\(([^\\)]+)\\)/', $text, $match)

上面的表达式工作正常,但我想一次性完成,而不是单独进行。 我猜是我的强迫症发作了:/

$out= preg_split('/[\(\)]/',$text)[1];

您可以使用捕获组:

$text = 'First string here(second here)';
if (preg_match('#^([^\(]+)\(([^\)]+)\)$#', $text, $match)) {
    print "$match[1]\n";
    print "$match[2]";
}

参见有关ideone的演示

暂无
暂无

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

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