
Lorenzo Bettini wrote:
John Maddock wrote:
Lorenzo Bettini wrote:
Hi
is there a way to specify open and closed parenthesis with a regular expression syntax? For instance, if I'd want to specify a single regular expression that matches both (foo) and [foo] and {foo}?
Probably not what you wanted, but:
[(\[{]foo[)\]}]
but this would match also {foo] which would wrong...
You could use Spirit, of course, using a function that recorded which type of bracket (pedantic point: "(", "), "[", "]", "{" and "}" are all brackets, but only "(" and ")" are parentheses) was found first and another function that tested whether the close bracket matched the open bracket; this way, you could even check for correctly nested brackets if you wanted to. The downsides with using Spirit are: * if you don't already know it, you'll have to learn a fair bit of Spirit in order to do this; * it will add quite a big chunk of code to your source for what is, after all, a simple parsing operation; * it will increase the size of your executable considerably; * it will (possibly) be slower than regex. I'd suggest seeing if anyone else comes up with a solution using regex, and writing your own simple parser by hand (not using Spirit) if not.