flyhoney Posted January 19, 2009 Share Posted January 19, 2009 Hello regex masters. I am parsing GIS Geometry objects and need a bit of help. I have a string that looks like this: MULTIPOLYGON(((-91.930386 40.80865,-91.930386 40.80865[etc...])),((-91.930179 40.808662,-91.930386 40.80865[etc...]))) It is a list of N polygons, so if N = 1, it looks like this: MULTIPOLYGON(((-91.930386 40.80865,-91.930179 40.808662[etc...]))) If N = 2, it looks like this: MULTIPOLYGON(((-91.930386 40.80865[etc...])),((-91.930179 40.808662[etc...]))) Etc... The current expression I am using only handles the N = 1 case: /POLYGON[\(]+(.*)[\)]+/i This effectively grabs all the coordinates in a string so I can explode() them. Can you freaks help me expand this regex to account for N > 1? Let me know if none of that made sense. Quote Link to comment Share on other sites More sharing options...
.josh Posted January 19, 2009 Share Posted January 19, 2009 other than the fact it is variable length, is that the only thing on the line, or is this thing being regexed from within a larger string? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted January 19, 2009 Author Share Posted January 19, 2009 It's the only thing in the string. I think I've figured it out actually, this seems to be doing what I want: [\(]+(.*?)[\)]+ (using preg_match_all) Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted January 19, 2009 Share Posted January 19, 2009 I think I've figured it out actually, this seems to be doing what I want: [\(]+(.*?)[\)]+ You can actually use [(]+([^)]+) -or- \(+([^)]+) In your example, note that in a character class, you do not need to escape characters like (, as those metacharacters are striped of their special abilities and thus rendered as literals when inside a class.. In fact, there are not many meta characters that survive as such when inside a class, and this largely depends on their location.. By example, ^ must be the first one listed within the class to make the class a negated one, otherwise, it is treated as a literal.. the dash must be placed as either the very first or very last character to be a dash literal, otherwise, it creates a range. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted January 19, 2009 Author Share Posted January 19, 2009 Thanks for the advice nrg_alpha. I was unaware of that behavior. I ended up tweaking the expression some more, but I'm away from my home computer and can't remember what I changed it to, but either way, works like a gem. Thanks guys. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.