Parsing JSON with a single Regex

This video is in English.
Length: 29:34
Source: Houston Perl Mongers on the 2013-10-17.
Speaker: brian d foy

In September 2012 Randal Schwartz posted a JSON parser as a single Perl Regex on PerlMonks.

In this presentation by brian d foy explains the regex at the meeting of the Houston Perl Mongers

See also the write-up about the meeting and the slides brian d foy put online.

Mastering Perl Advanced Regular expressions is a chapter of that book

Some of the items

  • Uses grammars: (?(DEFINED))
  • Recurses: (?&KV), et alia
  • Runs code during the regex: (?{ ... })
  • Builds up a data structure: $^R
  • At the end, replaces the string with a data structure: (?{ $_ = $^R->[1] })

The comments

  • Named captures: (?
  • Backreference to \1 ($1)
  • Independent subpattern reference, (?PARNO) for example: (?1)
  • Named capture backreference \g{LABEL}
  • ++ Is a non-backtracking + quantifier
  • Independent subpattern reference (?R) - the entire pattern
  • Grammars - (?(DEFINE)...)
  • Last captured substring $^N (the previous captured substring)
  • Grammars - (?&NAME)
  • Result of last (?{...}) goes into $^R
  • (?&VALUE)