PHP 正则表达式组捕获

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5642836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:03:49  来源:igfitidea点击:

PHP regex groups captures

phpregex

提问by carlosdubusm

I have the following regex:

我有以下正则表达式:

\[([^ -\]]+)( - ([^ -\]]+))+\]

This match the following successfully:

这成功匹配以下内容:

[abc - def - ghi - jkl]

BUT the match is:

但比赛是:

Array
(
    [0] => [abc - def - ghi - jkl]
    [1] => abc
    [2] =>  - jkl
    [3] => jkl
)

What I need is something like this:

我需要的是这样的:

Array
(
    [0] => [abc - def - ghi - jkl]
    [1] => abc
    [2] =>  - def
    [3] => def
    [4] =>  - ghi
    [5] => ghi
    [6] =>  - jkl
    [7] => jkl
)

I'm able to do that in C# looking at the groups "captures". How can I do that in PHP?

我可以在 C# 中查看组“捕获”来做到这一点。我怎样才能在 PHP 中做到这一点?

采纳答案by Amadan

This is not the job for the regexp. Match against \[([^\]]*)\], then splitthe first capture by the " - ".

这不是正则表达式的工作。对战\[([^\]]*)\]split则由第一个捕获" - "

<?php                                                                       
  $str = "[abc - def - ghi - jkl]";
  preg_match('/\[([^\]]*)\]/', $str, $re);
  $strs = split(' - ', $re[1]);
  print_r($strs);
?>

回答by drudge

Assuming the tokens in your sample string never contain spaces, and are alphanumeric:

假设您的示例字符串中的标记从不包含空格,并且是字母数字:

<?php
    $pattern = "/([\w|\d])+/";
    $string = "[abc - 123 - def - 456 - ghi - 789 - jkl]";
    preg_match_all($pattern, $string, $matches);
    print_r($matches[0]);
?>

Output:

输出:

Array
(
    [0] => abc
    [1] => 123
    [2] => def
    [3] => 456
    [4] => ghi
    [5] => 789
    [6] => jkl
)

回答by ThorSummoner

SPL preg_match_allwill return regex groups starting on index 1 of the $matchesvariable. If you want to get only the second group you can use $matches[2]for example.

SPL preg_match_all将返回从$matches变量的索引 1 开始的正则表达式组。例如,如果您只想获得第二组,则可以使用$matches[2]

Syntax:

句法:

$matches = array(); 
preg_match_all(\
    '/(He)\w+ (\w+)/', 
    "Hello world\n Hello Sunshine", 
    $matches
); 
var_dump($matches);

Result:

结果:

array(3) {
  [0] =>
  array(2) {
    [0] =>
    string(11) "Hello world"
    [1] =>
    string(14) "Hello Sunshine"
  }
  [1] =>
  array(2) {
    [0] =>
    string(2) "He"
    [1] =>
    string(2) "He"
  }
  [2] =>
  array(2) {
    [0] =>
    string(5) "world"
    [1] =>
    string(8) "Sunshine"
  }
}


P.S. This answer is posted for the context of the question title after being directed here by a Google search. This was the information I was interested in when searching for this topic.

PS 在通过 Google 搜索定向到此处后,此答案是针对问题标题的上下文发布的。这是我在搜索此主题时感兴趣的信息。

回答by Andrew

To group your matches, use parenthesize. EG:

要将匹配项分组,请使用括号。例如:

$string = 'bob';
preg_match('/bob/', $string, $matches);

$matcheswill be ['bob']

$matches将会 ['bob']

preg_match('/(b)(o)(b)/', $string, $matches);

$matcheswill be ['bob','b','o','b']

$matches将会 ['bob','b','o','b']