gpt4 book ai didi

带括号的 PHP 正则表达式

转载 作者:可可西里 更新时间:2023-11-01 00:35:45 25 4
gpt4 key购买 nike

我有以下字符串:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com