How to match nested curly braces using regular expressions in PHP?

I have a LaTeX document that I want to map. And I need a RegEx match that matches the following:

\ # the backslash in the beginning
[a-zA-Z]+ #a word
(\{.+\})* # any amount of {something}

However, this is a ploy;

In the last line, this 1. must be greedy, and 2. must have an appropriate amount {}within itself.

Value, if I have a string \test{something\somthing{9}} this will match everything. And it should be in that order ( {}). So that it does not match the following:

\ LaTeX {} is a document preparation system for \ TeX {}

only

\ LaTeX {}

and

\ TeX {}

Help someone? Maybe someone has a better idea for matching? Should I use regular expressions?

+5
3

:

$input = "\LaTeX{} is a document preparation system for the \TeX{}
\latex{something\somthing{9}}";

preg_match_all('~(?<token>
        \\\\ # the slash in the beginning
        [a-zA-Z]+ #a word
        (\{[^{}]*((?P>token)[^{}]*)?\}) # {something}
)~x', $input, $matches);

\LaTeX{}, \TeX{} \latex{something\somthing{9}}

+2

PHP , . , , , LaTeX, { } , .

:

$text = 'This is a \LaTeX{ foo { bar { ... } baz test {} done } } document
preparation system for the \TeX{a{b{c}d}e{f}g{h}i}-y people out there';
preg_match_all('/\\\\[A-Za-z]+(\{(?:[^{}]|(?1))*})/', $text, $matches, PREG_SET_ORDER);
print_r($matches);

:

Array
(
    [0] => Array
        (
            [0] => \LaTeX{ foo { bar { ... } baz test {} done } }
            [1] => { foo { bar { ... } baz test {} done } }
        )

    [1] => Array
        (
            [0] => \TeX{a{b{c}d}e{f}g{h}i}
            [1] => {a{b{c}d}e{f}g{h}i}
        )

)

:

\\\\         # the literal '\'
[A-Za-z]+    # one or more letters
(            # start capture group 1   <-----------------+
  \{         #   the literal '{'                         |
  (?:        #   start non-capture group A               |
    [^{}]    #     any character other than '{' and '}'  |
    |        #     OR                                    |
    (?1)     #     recursively match capture group 1  ---+
  )          #   end non-capture group A
  *          #   non-capture group A zero or more times
  }          #   the literal '}'
)            # end capture group 1 
+2

, , . ( , ) , , . , , . - {*[^{}]*}* , script, , .

: . , .

-1

All Articles