Sunday, 15 September 2013

Regex: remove double parenthesis but preserve singles

Regex: remove double parenthesis but preserve singles

I have a bunch of co-ordinate data coming back in the following format:
TYPE ((lat long, lat long), (lat long, lat long), (lat long))
I'm trying and failing at creating a regex that will remove anything
except digits, spaces, commas, hypens, and SINGLE parenthesis; I can't for
the life of me figure out how to remove double parenthesis but not single.
I don't need to replace the double parenthesis with singles, just remove
them.
What I've come up with so far is:
preg_replace('/[^0-9,.-\s\(\)/', '', $val);
But this removes ALL brackets, which is not what I want. I've been trying
with lookahead/lookbehind assertions but can't quite seem to get it
right...

No comments:

Post a Comment