tags:

views:

56

answers:

3

I'm not very good with regex, but here's what I got (the string to parse and the regex are on this page) http://rubular.com/r/iIIYDHkwVF

It just needs to match that exact test string

+1  A: 

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).

Greg Bacon
Yeah I just wrote that but I cant figure out how to match all of the keys and values
c00lryguy
http://gskinner.com/RegExr/ is what I use and is very good for breaking down a regular expression
c00lryguy
@c00lryguy See updated answer.
Greg Bacon
+1  A: 

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 }
Bolo
I can work with this! Thank you very much
c00lryguy
A: 

^"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 +).

Piotr Kalinowski