Valkyrie Savage
Valkyrie Savage

Reputation: 615

How does one comment regular expressions in c++?

Is it possible to write comments inline for regular expressions in C++? I find the multiline commentable regexes in Python to be great for readability and maintainability later on. Otherwise, what is the canonical way to write comments for regexes?

Sorry, this is probably a duplicate question, but all my searching was just turning up C++ regexes to find C-style comments...

Upvotes: 3

Views: 171

Answers (3)

Josh Kelley
Josh Kelley

Reputation: 58352

The answer depends on what regex library you're using. PCRE? std::regex? Boost.Regex? Something else?

Boost.Regex's Perl syntax supports comments, although the syntax looks not as nice as Python's. PCRE should support Python-style comments. I don't see anything about comments in std::regex.

Otherwise, you have a couple of options.

First, as @StilesCrisis says, string concatenation is very straightforward.

Second, in C++11, you can create a user-defined literal that understands and removes comments.

Upvotes: 0

James Kanze
James Kanze

Reputation: 153909

It depends on what level you want to comment. A block comment above the regular expression, explaining what it's trying to match, is probably the most important. But if you have somethihng complicated, you can break the expression down in to multiple string literals (with nothing but white space and comments between them) and comment these:

std::regex matchFloatingPointLiteral(
        "\\d+\\.\\d*(?:[Ee][+-]\\d+)?"    //  matches forms starting with digit
    "|" "\\.\\d+(?:[Ee][+-]\\d+)?"        //  matches forms starting with a .
    "|" "\\d+?:[Ee][+-]\\d+"              //  matches forms with no decimal
    );

This can be useful when your regular expression starts getting complicated.

Upvotes: 3

StilesCrisis
StilesCrisis

Reputation: 16290

If the regex is being entered as a regular C++ string, it's easy. Two quoted strings sitting next to one another in C++ automatically get merged into one big string, even if comments are intermingled. So:

"^Hello "  /* this is the first part of my regex */
"World!$"  /* this is the second part of my regex */

parses identically to:

"^Hello World!$" /* this is the whole regex on one line */

Upvotes: 4

Related Questions