带括号的 PHP 正则表达式

标签 php regex

我有以下字符串:

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/

相关文章:

php - 如何处理 file_get_contents() 中的 403 错误?

PHP、MySQL 错误 : Column count doesn’t match value count at row 1

正则表达式删除尖括号后的文本

php - 正则表达式 : Remove all comments from html file BUT preserve same number of lines

java - 按数字正则表达式分割字符串

Java 点不匹配 'any character'

php - MySQL 数据库事务中的 Codeception 验收测试

php - 找不到 Symfony2.7.0 app_dev.php 路由

php - 如何对两个以上的表使用连接?

Javascript 将电话号码填充到 3 个输入文本框中,名称相同但没有 id