0

I have a configuration file that contains a list of keywords. Following each keyword is a block that contains one or more values, each of which is surrounded by quotes. For example:

SrcDir { "D:\Temp\Input\" }
WorkDir { "D:\Temp\Work\" }
Folders {
  "Standard Color Workflow"
  "Normal"
  "Fast"
}

I am trying to write a regular expression that will return as a capture group the keyword and then a list of its corresponding value(s).

I started out using ([\w]+)\s\{([^}\n]+)}\n but it obviously doesn't work on the Folders section of the above sample, nor can this regex return multiple values even if they're all on the same line.

Any help with this would be greatly appreciated.

Tony
  • 1,401
  • 9
  • 11

1 Answers1

0

You're basically there. You just need to eliminate your \n references. As in:

([\w]+)\s\{([^}]+)}

Now, that means in the third case, that all those fields will have their \n's embedded inside the captured group.

Although now, I see you have a comment about "nor can this regex return multiple values even if they're all on the same line". So, perhaps I'm not understanding your problem.

--- EDIT:

To get each of these into their own capture group, To specify the quotes out separately. Try this:

([\w]+)\s\{(\s*"([^}"]+)"\s*)+}  -- doesn't work

--- EDIT 2:

My example of getting them each into their group doesn't work. And it won't. StackOverflow: 5018487.

I suggest using my original regex, take the complete set of strings found inside the {'s and then do a second regex search on that matched text to pull out each "text".

Thanks, I learned something today!

Community
  • 1
  • 1
Mike Ryan
  • 4,234
  • 1
  • 19
  • 22
  • Ok, that's close. Is there any way to have the regex split up the values into their own capture groups? In order words, the three values in the Folders group would be their own capture group, and any white space between them removed? – Tony Feb 15 '12 at 16:17
  • I tried something similar to that, but it only ever returns the last item in the list (e.g. "Fast" in the Folders example). – Tony Feb 15 '12 at 16:32
  • Ok, I see that it can't work. I appreciate you looking into this, Mike. I will work on implementing your suggestion. – Tony Feb 15 '12 at 16:46
  • Thanks much -- And welcome to stackoverflow. The etiquette here would be to accept the answer. Increases both our reps, and encourages others to answer further questions of yours. – Mike Ryan Feb 15 '12 at 16:50