我有以下字符串:
test 13 (8) end 12 (14:48 IN 1ST)
我需要的输出是:
14:48 IN 1ST
或括号内的所有内容到字符串的末尾。
我不需要,第一组括号内的 8。字符串中可以有多组括号。我只需要考虑输入字符串最后一组括号内的所有内容。
最佳答案
正则表达式解释
.* Go to last
\( Stars with (
([^)]*) 0 or more character except )
\) Ends with
preg_match
$str = "test 13 (8) end 12 () (14:48 IN 1ST) asd";
$regex = "/.*\(([^)]*)\)/";
preg_match($regex,$str,$matches);
$matches
array (
0 => 'test 13 (8) end 12 () (14:48 IN 1ST)',
1 => '14:48 IN 1ST',
)
接受空 preg_match_all
$str = "test 13 (8) end 12 () (14:48 IN 1ST) asd";
$regex = "/\(([^)]*)\)/";
preg_match_all($regex,$str,$matches);
$matches
array (
0 =>
array (
0 => '(8)',
1 => '()',
2 => '(14:48 IN 1ST)',
),
1 =>
array (
0 => '8',
1 => '',
2 => '14:48 IN 1ST',
),
)
不接受空的 preg_match_all
$str = "test 13 (8) end 12 () (14:48 IN 1ST) asd";
$regex = "/\(([^)]+)\)/";
preg_match_all($regex,$str,$matches);
$matches
array (
0 =>
array (
0 => '(8)',
1 => '(14:48 IN 1ST)',
),
1 =>
array (
0 => '8',
1 => '14:48 IN 1ST',
),
)
关于带括号的 PHP 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8220180/