
Eric Niebler wrote:
John Maddock wrote:
Lorenzo Bettini wrote:
do you happen to know whether there are any specifications for regular expressions for matching parenthesis (I mean in any regexp frameworks or libraries)? Not really no, but I have another idea, assuming the x-modifier is on:
(?x) (?: (\() |(\[) |(\{) ) foo (?: (?(1)\) |(?:(?(2)\] |(?:\} ))))
The idea is to use conditional expressions to check which opening backet matched and then react accordingly. You'll need to check I've got the ('s and )'s matching up 'cos I lost count while typing in :-(
You can do something similar with xpressive (alternate regex engine which will be in boost 1.34):
cregex rx = cregex::compile( "(\\(()|\\[()|\\{())foo(\\4\\}|\\3\\]|\\2\\))");
if(regex_match("(foo)", rx)) { std::cout << "match!\n"; } if(!regex_match("(foo]", rx)) { std::cout << "no match!\n"; }
For each alternate, you create an empty capture with "()". Then you match that capture again in the balanced alternate on the other side. Backreferences (even empty ones) only match if their capture participated in the match.
mhh... this looks nice :-) do you happen to know whether empty captures can be specified in boost::regex (I cannot try it at the moment, but I'll do it asap) thanks Lorenzo -- Lorenzo Bettini, PhD in Computer Science, DSI, Univ. di Firenze ICQ# lbetto, 16080134 (GNU/Linux User # 158233) HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com http://www.gnu.org/software/src-highlite http://www.gnu.org/software/gengetopt http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net