Search code examples
c++regexc++11subroutine

Do any of the regex syntaxes in the standard library support (?(DEFINE) for subpattern referencing?


With PCRE you can define subpatterns which can be referenced later on. Here's a trivial example:

# start delimiter
/

# define non-matching subpatterns, is this supported by <regex>?
(?(DEFINE)
  (?<alpha> [A-Za-z])
  (?<num> [0-9])
)

# actual pattern, referencing subpattern definitions
^ (?&alpha){2} (?&num){2} $

#end delimiter and extended flag to ignore whitespace in pattern
/x

Do any of the regex syntaxes from the standard library <regex> support this, or is this really a PCRE-specific feature, perhaps?

I'm working with C++11.


Solution

  • The <regex> library supports the following grammars:

    • ECMAScript: The Modified ECMAScript regular expression grammar;

    • basic: The basic POSIX regular expression grammar;

    • extended: The extended POSIX regular expression grammar;

    • awk: The regular expression grammar used by the awk utility in POSIX;

    • grep: The regular expression grammar used by the grep utility in POSIX. This is effectively the same as the basic option with the addition of newline '\n' as an alternation separator;

    • egrep: The regular expression grammar used by the grep utility, with the -E option, in POSIX. This is effectively the same as the extended option with the addition of newline '\n' as an alternation separator in addtion to '|'.

    Unfortunately, none of them support this feature.

    More information on cppreference.com.