0

I'm trying to use named subexpressions to replace multiple patterns in the input string using boost::regex_replace and named subexpressions.

I use the following code:

std::string s="Sun Fun Gun Tic Tac Toe ;$!";
boost::regex expr("(?<from1>.un)|(?<from2>[:;!])"); //Throws the error
std::string fmt("(?'from1'nm)(?'from2'sp)");
std::string s2 = boost::regex_replace(s,expr,fmt,boost::match_default|boost::format_all);
std::cout<<s2<<std::endl;

However, when run thos throws the following error:

terminate called after throwing an instance of 'boost::regex_error'
what():  Invalid preceding regular expression
Aborted

Please guide as to what I could be doing wrong?

Mindstorm
  • 443
  • 1
  • 5
  • 12

1 Answers1

0

Don't know about Boost, but std::regex (in TR1 and C++11), which was based on Boost, doesn't support named subexpressions.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • The following page describes support for Perl Regex syntax in Boost Libraries: [link](http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html). So, I believe it does support the same. – Mindstorm Sep 03 '12 at 12:47
  • @Mindstorm - thanks for the link. They changed it after the standard adopted it (the standard doesn't do perl; the default is ECMAScript, which is not a synonym for "perl"). – Pete Becker Sep 03 '12 at 12:53
  • @Mindstorm - this doesn't affect the line that's marked "Throws the error", but it looks like the format string should be something like `"$+{from1}nm$+{from2}sp"`. That is, `$+{NAME}` "[o]utputs whatever matched the sub-expression named 'NAME'". – Pete Becker Sep 03 '12 at 12:59
  • Changing to $+{NAME} didn't help either. It throws the same error. The reason why I said that line throws the error is because the error occurs when I try to define an expression using that string, irrespective of whether I define the following format string and do the replace or not. – Mindstorm Sep 03 '12 at 14:12
  • @Mindstorm - yes, that's what I meant to imply in my previous comment. I was just showing off the results of my research, starting with the link you provided. – Pete Becker Sep 03 '12 at 14:54