I need to write a regular expression to match a quoted string in which the
double quote character itself is represented by 2 double quotes. For
example:
"beginning ""nested quoted string"" end"
Quotes aren't special characters in any of the regular expression
grammars supported by TR1, so the regular expression is
""(.*)""[/QUOTE]
Close, but no cigar. That will match a string that has quotes at
the begin and end but the quotes inside (if any) might not be
doubled.
If a quoted string "A" is matched simply by /"[^"]*"/
or in c++, "\"[^\"]*\""
A string with possible double quotes in it "A""A" is just a series
of the above. So it is /("[^"]*")*/, or in c++ "(\"[^\"]*\")*".
#include <string>
#include <iostream>
#define BOOST_REGEX_NO_FILEITER
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
bool check (std::string what)
{
static const boost::regex expr("(\"[^\"]*\")*" );
boost::smatch tokens;
bool result = regex_match(what, tokens, expr);
cout << result << "\t(" << what << ")\n";
return result;
}
int main(int argc, char* argv[])
{
check("\"I am a duck\"");
check("\"I am a \"\" duck\"");
check("\"I \"\"am\"\" a \"\" duck\"");
check("I am not a duck");
check("\"I am \"not\" a duck\"");
check("I am \"not\" a duck");
check("\"I am not \" a duck\"");
}