用于匹配子表达式的正则表达式

RegEx for matching subexpression

当我使用

这样的正则表达式时
std::regex midiNoteNameRegex("([cdefgab])([b#]{0,1})([0-9]))|([0-9]{3})|([A-Z0-9]{2})");

有三个顶级子表达式由“|”连接以其中一个匹配的模式。 有没有办法告诉哪个?除了依次测试它们之外?

如果我使用命名子表达式会很容易,但是 C++ 中没有命名子表达式。

如何解决这个问题?

我没有明确的答案,但我相信答案很可能是否定的。

命名捕获组不是必需的功能:http://www.cplusplus.com/reference/regex/ECMAScript/

命名捕获组的实现可能并不简单,可能会降低正则表达式引擎的性能。

找到另一个 post 在这个问题上同意我的观点:C++ regex: Which group matched?

给定正则表达式中的组,这只是匹配对象的平面搜索,
在 C++ 中是标志 (int) 检查,没有明显的开销。

    ( [cdefgab] )                 # (1)
    ( [b#]{0,1} )                 # (2)
    ( [0-9] )                     # (3)
 |  ( [0-9]{3} )                  # (4)
 |  ( [A-Z0-9]{2} )               # (5)

以及可能的用法

wregex MyRx = wregex( "([cdefgab])([b#]{0,1})([0-9])|([0-9]{3})|([A-Z0-9]{2})", 0);

wstring::const_iterator start = str.begin();
wstring::const_iterator end   = str.end();
wsmatch m;

while ( regex_search( start, end, m, MyRx ) )
{
    if ( m[1].matched )       
        // First alternation
    else
    if ( m[4].matched )       
        // Second alternation
    else
    if ( m[5].matched )       
        // Third alternation
    start = m[0].second;
}