Could someone help me parse this string with regex?
I'm not very good with regex, but here's what I got (the string to parse and the regex ar开发者_运维知识库e on this page) http://rubular.com/r/iIIYDHkwVF
It just needs to match that exact test string
The regular expression is
^"AddonInfo"$(\n\s*)+^\{\s*
It's looking for
^"AddonInfo"$— a line containing only"AddonInfo"(\n\s*)+— followed by at least one newline and possibly many blank or empty lines^\{\s*— and finally a line beginning with{followed by optional whitespace
To break down a regular expression into its component pieces, have a look at an answer that explains beginning with the basics.
To match the entire string, use
^"AddonInfo"$(\n\s*)+^\{(\s*".+?"\s+".+?"\s*\n)+^\}
So after the open curly, you're looking for one or more lines such that each contains a pair of quote-delimited simple strings (no escaping).
This one works:
^"AddonInfo"[^{]*{[^}]*}
Explanation:
^"AddonInfo"matches"AddonInfo"in the beginning of a line[^{]*matches all the following non-{characters{matches the following{[^}]*matches all the following non-}characters}matches the following}
^"AddonInfo"(\s*)+^\{\s*(?:"([^"]+)"\s+"([^"]*)"\s+)+\}
You will get $1 to point into first key, $2 first value, $3 second key, $4, second value, and so on.
Notice that key is to be non-empty ("([^"]+"), but value may be empty (uses * instead of +).
加载中,请稍侯......
精彩评论