简体   繁体   English

获取大括号之间出现的所有单词

[英]Get all occurrences of words between curly brackets

I have a text like: 我有一个文字像:

This is a {demo} phrase made for {test}

I need to get 我需要得到

demo  
test

Note: My text can have more than one block of {} , not always two. 注意:我的文本可以包含多个{}块,而不是总是两个。 Example: 例:

This is a {demo} phrase made for {test} written in {English}

I used this expression /{([^}]*)}/ with preg_match but it returns only the first word, not all words inside the text. 我在preg_match使用了这个表达式/{([^}]*)}/但它只返回第一个单词,而不是文本中的所有单词。

Use preg_match_all instead: 请改用preg_match_all

preg_match_all($pattern, $input, $matches);

It's much the same as preg_match , with the following stipulations: 它与preg_match非常相似,具有以下规定:

Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags. 将所有匹配的主题搜索到模式中给出的正则表达式,并按照flags指定的顺序将它们放入匹配项中。

After the first match is found, the subsequent searches are continued on from end of the last match. 找到第一个匹配后,后续搜索将从最后一个匹配结束后继续。

Your expression is correct, but you should be using preg_match_all() instead to retrieve all matches. 您的表达式是正确的,但您应该使用preg_match_all()来检索所有匹配项。 Here's a working example of what that would look like: 这是一个可行的例子:

$s = 'This is a {demo} phrase made for {test}';

if (preg_match_all('/{([^}]*)}/', $s, $matches)) {
        echo join("\n", $matches[1]);
}

To also capture the positions of each match, you can pass PREG_OFFSET_CAPTURE as the fourth parameter to preg_match_all . 要同时捕获每个匹配的位置,您可以将PREG_OFFSET_CAPTURE作为第四个参数传递给preg_match_all To use that, you can use the following example: 要使用它,您可以使用以下示例:

if (preg_match_all('/{([^}]*)}/', $s, $matches, PREG_OFFSET_CAPTURE)) {
        foreach ($matches[1] as $match) {
            echo "{$match[0]} occurs at position {$match[1]}\n";
        }
}

As the { and } are part of regex matching syntax, you need to escape these characters: 由于{}是正则表达式匹配语法的一部分,因此您需要转义这些字符:

<?php
$text = <<<EOD
this {is} some text {from}
which I {may} want to {extract}
some words {between} brackets.
EOD;
preg_match_all("!\{(\w+)\}!", $text, $matches);
print_r($matches);
?>

produces 产生

Array
(
    [0] => Array
        (
            [0] => {is}
            [1] => {from}
            [2] => {may}
            [3] => {extract}
            [4] => {between}
        )
     ... etc ...
)

This example may be helpful to understand the use of curly brackets in regexes: 此示例可能有助于理解正则表达式中大括号的使用:

<?php
$str = 'abc212def3456gh34ij';
preg_match_all("!\d{3,}!", $str, $matches);
print_r($matches);
?>

which returns: 返回:

Array
(
    [0] => Array
        (
            [0] => 212
            [1] => 3456
        )
)

Note that '34' is excluded from the results because the \\d{3,} requires a match of at least 3 consecutive digits. 请注意,结果中不包括'34',因为\\d{3,}需要至少3个连续数字的匹配。

Matching portions between pair of braces using RegEx, is less better than using Stack for this purpose. 使用RegEx匹配一对大括号之间的部分不如使用Stack用于此目的。 Using RegEx would be something like «quick and dirty patch», but for parsing and processing input string you have to use a stack. 使用RegEx会像“快速和脏的补丁”,但是对于解析和处理输入字符串,你必须使用堆栈。 Visit here for the concept and here for applying the same. 请访问此处获取该概念,并在此处应用相同的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM