我正在寻找一种从此字符串中获取以下数据的方法
DON'T FLOP - Rap Battle - Illmaculate Vs Tony D
DON'T FLOP - Rap Battle - $var1 Vs $var2
所以我最终可以得到$var3 = $var1 Vs $var2
问题是对手的名字可以包含多个单词,而我可以一直到vs右侧的对手的句子末尾,我没有办法分隔对手名字的开头,是吗?
如何从 -、在 vs 处停止并重新开始 $var 2 直到句子末尾进行检查?
一个
非贪婪的捕获组(.+?)
Vs
左侧,在-
和空格后面应该抓住名字。只要您始终在-
后面有空间,这应该可以正常工作。 如有必要,'s+
允许多个空格。
$pattern = '/Rap Battle -'s+(.+?)'s+Vs's+(.+)$/';
$string = "DON'T FLOP - Rap Battle - Illmaculate Vs Tony D";
preg_match($pattern, $string, $matches);
var_dump($matches);
array(3) {
[0]=>
string(34) "Rap Battle - Illmaculate Vs Tony D"
[1]=>
string(11) "Illmaculate"
[2]=>
string(6) "Tony D"
}
$var1 = $matches[1];
$var2 = $matches[2];
$text = "DON'T FLOP - Rap Battle - Illmaculate Vs Tony D";
$regex = '%Rap Battle - (.*?) Vs (.*)$%';
preg_match($regex, $text, $array);
$array[0] = entire string match.
$array[1] = first opponent.
$array[2] = 2nd opponent.