Created
August 14, 2012 16:00
-
-
Save tomotaka/3350489 to your computer and use it in GitHub Desktop.
recursive regexp pattern for JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# only works on 1.9+ | |
# base idea is from: | |
# - http://stackoverflow.com/questions/2583472/regex-to-validate-json | |
# - http://www.slideshare.net/takesako/shibuyapm16-regexpjsonvalidator | |
module JsonMatcher | |
NUMBER = /-? (?= [1-9]|0(?!\d) ) \d+ (\.\d+)? ([eE] [+-]? \d+)?/x | |
BOOLEAN = /true | false | null/x | |
STRING = /" ([^"\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* "/x | |
JS_NUMBER = /(?:-|\+)? (?=[1-9]|0(?!\d)) \d+ (\.\d+)? ([eE] [+-]? \d+)?/x | |
JS_BOOLEAN = BOOLEAN | |
JS_STRING_SINGLE_QUOTE = /' (?:\\'|\\\\|\r|\n|\s|[^\'])* '/x | |
JS_STRING_DOUBLE_QUOTE = / | |
" | |
(?: | |
\\\\ | | |
\\n | | |
\\f | | |
\\b | | |
\\r | | |
\\t | | |
\\' | | |
\\" | | |
\\[0-8]{3} | | |
\\x[0-9a-fA-F]{2} | | |
\\u[0-9a-fA-F]{4} | | |
\r | | |
\n | | |
\s | | |
[^\"] | |
)* | |
" | |
/x | |
JS_STRING = /#{JS_STRING_SINGLE_QUOTE} | #{JS_STRING_DOUBLE_QUOTE}/x | |
JS_BARE_STRING = /[0-9a-zA-Z_]+/ | |
# pattern of json | |
JSON = / | |
(?<json> | |
(?<number>#{NUMBER}) | | |
(?<boolean>#{BOOLEAN}) | | |
(?<string>#{STRING}) | | |
(?<array> | |
\[ \s* \g<json> (?: \s* , \s* \g<json> )* \s* \] | |
) | | |
(?<object> | |
\{ \s* (?<pair> \g<string> \s* : \s* \g<json> ) (?: \s* , \s* \g<pair> )* \s* \} | |
) | |
) | |
/x | |
SPACE = /\r|\n|\s/x | |
# pattern of JS expression which consists of combinatin of basic type value | |
JS_EXP = / | |
(?<jsexp> | |
(?<number>#{JS_NUMBER}) | | |
(?<boolean>#{JS_BOOLEAN}) | | |
(?<string>#{JS_STRING}) | | |
(?<array> | |
\[ #{SPACE}* \g<jsexp> (?:#{SPACE}* , #{SPACE}* \g<jsexp>|#{SPACE}*,#{SPACE}*)* #{SPACE}* \] | |
) | | |
(?<object> | |
\{ | |
#{SPACE}* | |
(?<pair> | |
(?:#{JS_BARE_STRING}|#{JS_STRING}) | |
#{SPACE}* | |
: | |
#{SPACE}* | |
\g<jsexp> | |
) | |
(?: #{SPACE}* , #{SPACE}* \g<pair>)* | |
#{SPACE}* | |
\} | |
) | |
) | |
/x | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!