Выберите блок YAML с помощью регулярного выражения - PullRequest
4 голосов
/ 09 июля 2011

У меня большой файл YAML, и я хочу выбрать весь узел с помощью регулярных выражений. Например:

Node1:
  Child:
    GrandChild: foo
Node2:
  AnotherChild:
    AnotherGrandChild: bar
Node3:
  LastChild:
    LastGrandChild: foo

Как я могу использовать регулярные выражения, чтобы выбрать все Node2 в приведенном выше примере, и вернуть:

Node2:
  AnotherChild:
    AnotherGrandChild: bar

Ответы [ 2 ]

4 голосов
/ 09 июля 2011

Поскольку все остальное в этом узле имеет отступ (если я правильно понимаю YAML), это работает, по крайней мере, для вашей строки примера:

$mask = '~(^%s:\n(?:^[ ].*\n?)*$)~m';
$pattern = sprintf($mask, 'Node2');
$r = preg_match($pattern, $yaml, $matches);
$node = reset($matches);

По крайней мере, на моем компьютере.Хотел сделать демо кодпада, но выдает ошибку.Будем проверять регулярные выражения.

Полноценно:

$yaml = <<<EOD
Node1:
  Child:
    GrandChild: foo
Node2:
  AnotherChild:
    AnotherGrandChild: bar
Node3:
  LastChild:
    LastGrandChild: foo
EOD;

$mask = '~
(                     # start matching group
  ^                   # a node start always at the beginning of a line 
  %s:                 # placeholder for sprintf for the nodname + :
  $                   # end of line for the nodename              
  \n
  (?:                 # non-matching group to hold all subsequent, indented lines
    ^                 # beginning of sublines
    (?:[ ]{2})+       # indentation is required, always a muliple of two spaces, non matching group
    .*\n?             # match anything else on that subsequent line, optionally the newline character
  )*                  # 0 or more subsequent, indented lines
)$                    # this ends a line, to not take over the newline of the last subsequent line (see \n? above).

# the following are modifiers:
# m - pcre multiline modifier (in php same as in perl)
# x - to allow spaces and the comments all over here ;)
~mx
';
$pattern = sprintf($mask, 'Node2');
$r = preg_match($pattern, $yaml, $matches);
$node = reset($matches);

var_dump($node);
3 голосов
/ 09 июля 2011

Вы, вероятно, хотите использовать библиотеку, например php-yaml .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...