|
|
От: |
Пельмешко
|
blog |
| Дата: | 26.11.09 13:37 | ||
| Оценка: | 13 (2) | ||
(?<name1-name2> subexpression)
(Balancing group definition.) Deletes the definition of the previously defined group name2 and stores in group name1 the interval between the previously defined name2 group and the current group. If no group name2 is defined, the match backtracks. Because deleting the last definition of name2 reveals the previous definition of name2, this construct allows the stack of captures for group name2 to be used as a counter for keeping track of nested constructs such as parentheses. In this construct, name1 is optional. You can use single quotes instead of angle brackets; for example, (?'name1-name2').
(?<br>\()\w*(?<-br>\))Совпадёт только со словом с скобках ^\s* # начало строки
(?:(?'br'\()\s*)* # набор открывающих скобок
(?<text>\w+) # текст в скобках
(?:\s*(?'-br'\)))* # набор закрывающих скобок
(?(br) # проверяем, есть ли несбалансированная открытая скобка?
(?!) # если да, то надо упасть (это заведомо ложный negative lookahead)
| # иначе
(?!\s*\)) # убеждаемся, что спереди нету ещё одной закрывающей
)
\s*$ # конец строки