+            // we will alloc the array later (only if really needed) but count
+            // the number of sub-expressions in the regex right now
+
+            // there is always one for the whole expression
+            m_nMatches = 1;
+
+            // and some more for bracketed subexperessions
+            for ( const wxChar *cptr = expr.c_str(); *cptr; cptr++ )
+            {
+                if ( *cptr == _T('\\') )
+                {
+                    // in basic RE syntax groups are inside \(...\)
+                    if ( *++cptr == _T('(') && (flags & wxRE_BASIC) )
+                    {
+                        m_nMatches++;
+                    }
+                }
+                else if ( *cptr == _T('(') && !(flags & wxRE_BASIC) )
+                {
+                    // we know that the previous character is not an unquoted
+                    // backslash because it would have been eaten above, so we
+                    // have a bar '(' and this indicates a group start for the
+                    // extended syntax
+                    m_nMatches++;
+                }
+            }