Skip to main content
GameDev.net gamedev.net
🔒 Locked

Regular Expressions help needed

Started by Scet Jul 31, 2007 at 5:46 PM 2 replies 1.5k views
Original Post
Scet
Scet
Hi, I'm trying to load Anim8or files using the RegEx class, but I'm not having much success(I really have no idea what I'm doing). I have a loader that works by reading in and trimming / splitting the strings "manually", but I'd like something a little cleaner. Anyway this is what I'm trying to parse:

header { 
 version { "0.95" } 
 build { "2007.04.02" } 
}
object { 
 name { \"Bob\" } 
}
It's basically a chunk name with brackets and data / sub-chunks inside. My current expression is "\w+\s+\{.*?\}"(in .NET), but it stops right on the first } so it returns "header { version { \"0.95\" }". I'd like an expression that returns at first two strings, the header and the object chunks. Any help is appreciated.
Zahlman
Zahlman
strict regular expressions are simply not powerful enough to handle the "balanced brackets" problem. I don't know if the .NET Regexes offer enough additional power, though.

I would do it recursively, building a tree of the results... iteratively find a token and '{', recurse, and find a '}', until there is no token before EOF (or the next '}', on a recursive call).
Scet
Scet
Thanks, I was afraid that was the case(couldn't find anything online). I thought it would be a feature people would want.

Anyway I was able to replace most of my code with just a few RegEx calls, it's a lot cleaner now. I was just hoping I wouldn't have to build the tree myself.
Winograd
Winograd
Quick googling showed up that .NET in fact has the possibility of handling balanced matching by something called balancing groups:
http://blogs.pingpoet.com/overflow/archive/2005/03/16/1376.aspx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.