[英]PHP - Regex to remove the string between parenthesis
I'm trying to split a filename into 3 parts here. 我正在尝试将文件名分为3部分。
Example: Artist - Title ( Mix ) or Artist - Title [ Mix ] 例如:艺术家-标题(Mix)或艺术家-标题[Mix]
My code so far. 到目前为止,我的代码。
preg_match('/^(.*) - (.*)\.mp3$/', $mp3, $matches);
$artist = $matches[1];
$title = $matches[2];
echo "File: $mp3" . "Artist: $artist" . "\n" . "Title: $title" . "<br />";
This is getting me the Artist and the Title. 这使我获得了艺术家和头衔。 The problem I have is that Mix is either between () or [ ].
我的问题是Mix在()或[]之间。 I'm not sure how to modify my regex in order to capture that part.
我不确定如何修改我的正则表达式以捕获该部分。
This isn't a 100% regex solution, but I think it's the most elegant you'll get. 这不是100%的正则表达式解决方案,但我认为这是您将获得的最优雅的解决方案。
Basically, you want to capture (anything)
or [anything]
, which can be represented as \\(.*\\)|\\[.*\\]
. 基本上,您想捕获
(anything)
或[anything]
,它们可以表示为\\(.*\\)|\\[.*\\]
。 Then, make that a capture group, and double escape it, to get (\\\\(.*\\\\)|\\\\[.*\\\\])
. 然后,创建该捕获组,并对其进行两次转义以获取
(\\\\(.*\\\\)|\\\\[.*\\\\])
。
Unfortunately, this captures the ()
or []
as well, so you have to strip those; 不幸的是,它也捕获了
()
或[]
,因此您必须去除它们; I simply used substr($matches[3], 1, -1)
to do the job: 我只是使用
substr($matches[3], 1, -1)
来完成这项工作:
$mp3 = "Jimmy Cross - I Want My Baby Back (Remix).mp3";
preg_match('/^(.*) - (.*) (\\(.*\\)|\\[.*\\])\.mp3$/', $mp3, $matches);
$artist = $matches[1];
$title = $matches[2];
$mix = substr($matches[3], 1, -1);
echo "File: $mp3" . "<br/>" . "Artist: $artist" . "<br/>" . "Title: $title" . "<br />" . "Mix: $mix" . "<br />";
Prints out: 打印输出:
File: Jimmy Cross - I Want My Baby Back (Remix).mp3
File:Jimmy Cross-我想要我的宝贝回来(Remix).mp3
Artist: Jimmy Cross歌手:Jimmy Cross
Title: I Want My Baby Back标题:我要我的宝贝回来
Mix: Remix混音:混音
Try '/^(.*) - ([^\\(\\[]*) [\\(\\[] ([^\\)\\]]*) [\\)\\]]\\.mp3$/'
尝试
'/^(.*) - ([^\\(\\[]*) [\\(\\[] ([^\\)\\]]*) [\\)\\]]\\.mp3$/'
However, this may not be the most efficient way to do it. 但是,这可能不是最有效的方法。
I would use named subpatterns for this specific case. 在这种情况下,我将使用命名子模式。
$mp3s = array(
"Billy May & His Orchestra - T'Ain't What You Do.mp3",
"Shirley Bassey - Love Story [Away Team Mix].mp3",
"Björk - Isobel (Portishead remix).mp3",
"Queen - Another One Bites the Dust (remix).mp3"
);
$pat = '/^(?P<Artist>.+?) - (?P<Title>.*?)( *[\[\(](?P<Mix>.*?)[\]\)])?\.mp3$/';
foreach ($mp3s as $mp3) {
preg_match($pat,$mp3,$res);
foreach ($res as $k => $v) {
if (is_numeric($k)) unset($res[$k]);
// this is for sanitizing the array for the output
}
if (!isset($res['Mix'])) $res['Mix'] = NULL;
// this is for the missing Mix'es
print_r($res);
}
will output 将输出
Array (
[Artist] => Billy May & His Orchestra
[Title] => T'Ain't What You Do
[Mix] =>
)
Array (
[Artist] => Shirley Bassey
[Title] => Love Story
[Mix] => Away Team Mix
)
Array (
[Artist] => Björk
[Title] => Isobel
[Mix] => Portishead remix
)
Array (
[Artist] => Queen
[Title] => Another One Bites the Dust
[Mix] => remix
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.