2010-01-14 19 views
5

Voglio restituire l'output "match" se il pattern "regular" è una sottostringa di variabile st. È possibile?boost regex sub-string match

int main() 
{ 
    string st = "some regular expressions are Regxyzr"; 

    boost::regex ex("[Rr]egular"); 
    if (boost::regex_match(st, ex)) 
    { 
    cout << "match" << endl; 
    } 
    else 
    { 
    cout << "not match" << endl; 
    } 
} 
+0

Avete provato? Se è così, cosa è successo? –

+0

sì, ottengo "non corrisponde" se st = "regolare" ottengo "corrispondenza" – bob

risposta

16

Il boost :: regex_match corrisponde solo all'intera stringa, probabilmente si preferisce boost :: regex_search.

7

regex_search fa quello che vuoi; regex_match è documentato come

determina se una data regolare espressione corrisponde tutto di una data sequenza di caratteri

(l'enfasi è l'URL originale sto citando da).

0

La tua domanda si risponde con l'esempio nella documentazione della libreria - boost::regex

approccio alternativo:

È possibile usare boost :: regex_iterator, questo è utile per il file ecc

string[0], 
string[1] 

sotto analisi indica inizio e fine iteratore.

Es:

boost::regex_iterator stIter(string[0], string[end], regExpression) 
boost::regex_iterator endIter 

for (stIter; stIter != endIter; ++stIter) 
{ 
    cout << " Whole string " << (*stIter)[0] << endl; 
    cout << " First sub-group " << (*stIter)[1] << endl; 
} 

}

+0

La risposta accettata in questa pagina spiega meglio sregex_iterator http://stackoverflow.com/questions/2593288/how-to-use-c -boosts-regex-iteratore – syam