Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions C++ Qt

Tags:

c++

regex

qt

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 to print out test and match the word test within the string but it doesn't seem to be doing that. Could anyone shed some light here? Using QT.

like image 869
Dave Z Avatar asked Aug 02 '11 04:08

Dave Z


2 Answers

You don't have any capturing parens in your regex so there is no capture group 1. Try this instead:

QRegExp rx("\\b(test)\\b");
like image 170
Asaph Avatar answered Nov 15 '22 01:11

Asaph


Replace rx.cap(1) with rx.cap(0) The entire match has index 0.

like image 40
tim Avatar answered Nov 15 '22 01:11

tim