2014-12-26 13 views
0

我的應用程序具有用戶定義的正則表達式模式,最多可包含3個捕獲組。有沒有更好的方法來實現下面的代碼?PCRE PartialMatch組數

std::string glyph1, glyph2, glyph3; 

switch (regex.NumberOfCapturingGroups()) 
{ 
    case 0: 
    default: 
     found = regex.PartialMatch(word); 
     break; 
    case 1: 
     found = regex.PartialMatch(word, &glyph1); 
     break; 
    case 2: 
     found = regex.PartialMatch(word, &glyph1, &glyph2); 
     break; 
    case 3: 
     found = regex.PartialMatch(word, &glyph1, &glyph2, &glyph3); 
     break; 
} 

if (found) { 
    // ... 
} 

不幸的是,返回的值是假的,如果正則表達式會匹配,但有少捕獲組隨後要求。

回答

0

您可以使用列表或向量來解決此問題。而不是超載的功能使參數vector<std::string> glyphint n

vector<std::string> glyph; 
regex.PartialMatch(word, glyph, regex.NumberOfCapturingGroups()); 
if (found) { 
    // ... 
} 

然後只需使用glyph[i]來訪問元素。

+0

雖然我喜歡這個想法,但我確定這不是pcrecpp的實現方式(pcre-8.36中沒有這個方法定義),我真的不想把自己的代碼添加到pcre中。 –