Qt-interest Archive, March 2002
Fwd: QRegExp captured text
Message 1 in thread
I've already posted this to qt-bugs, but since I've had no response yet I
thought I'd try qt-interest. If someone at Trolltech is looking, please be
aware that I've posted to both addresses, since it caused some problem last
time I posted to both lists.
I've got some more questions concerning Regular Expressions and captured
text.
The following test program
#include <qregexp.h>
#include <qstring.h>
int main(int argc,char *argv)
{
QRegExp rx("comm ((:?-\\w\\s*)*) (param)");
qDebug(QString("search=%1").arg(rx.search("comm -a -b -c param")));
qDebug(QString("matchedLength=%1").arg(rx.matchedLength()));
for(int i=0;i<10;i++)
{
qDebug(QString("cap(%1)=\"%2\"").arg(i).arg(rx.cap(i)));
}
}
produces the following output
search=0
matchedLength=19
cap(0)="comm -a -b -c param"
cap(1)="-a -b -c"
cap(2)="-c"
cap(3)="param"
cap(4)=""
cap(5)=""
cap(6)=""
cap(7)=""
cap(8)=""
cap(9)=""
Given that the regular expression contains only two groups to be captured
((:?-\\w\\s*)*) and (param) I would expect list of captured strings to
contain three entries. The complete matched text (comm -a -b -c param), the
text caputered by the first grouping (-a -b -c) and the text captured by the
second group (param). As you can see cap(2) returns -c which I did not
expect, is this a bug or am I still mis-understanding the workings of the
text capture.
I'm using Qt 3.0.3
Thanks
Bob Stafford
-------------------------------------------------------