C ++ Qt Regular Expressions

QRegExp rx("\\btest\\b"); rx.indexIn("this is a test string"); QString captured = rx.cap(1); std::string capturedstr = captured.toUtf8().constData(); std::cout << capturedstr; 

I wanted the above test to print and match the test word in a string, but it doesn't seem to be that way. Can anyone shed some light here? Using QT.

+7
source share
2 answers

You have no captures in your regex, so there is no capturing group 1. Try this instead:

 QRegExp rx("\\b(test)\\b"); 
+9
source

Replace rx.cap(1) with rx.cap(0) The whole match is index 0.

+1
source

All Articles