PDA

View Full Version : Search for QRegExp in a QString



Abc
12th August 2008, 12:30
I have a tabbed interface in my application. I am getting the contents of the focused tab in a string variable as
QString myString = currentTab->getTextEdit()->toPlainText();

Now, I want to search this string for the occurrence of some other strings which I have defined as a regular expression and for each occurrence, I want to prepend a character to the occurrence. Please let me know how to find every occurrence of regular expression in myString.

jacek
12th August 2008, 18:01
See QString::indexOf().

Abc
13th August 2008, 06:40
Using indexOf() I am able to prepend the required character only to the first occurrence of the RegExp in myString. How can I do the same for all the occurrences??

jacek
13th August 2008, 07:25
How can I do the same for all the occurrences??
Invoke indexOf() once more, but this time use the second parameter and return value to skip the previous occurence.

Abc
13th August 2008, 08:32
This does not work if there are multiple occurrences of the same RegExp and it is not even known how amny times a particular RegExp would occur. How can I find multiple occurrences and make the required change??

spirit
13th August 2008, 08:40
did you see this example?


QRegExp rx("(\\d+)");
QString str = "Offsets: 12 14 99 231 7";
QStringList list;
int pos = 0;

while ((pos = rx.indexIn(str, pos)) != -1) {
list << rx.cap(1);
pos += rx.matchedLength();
}

Abc
13th August 2008, 09:31
Thanks works now!! :)