PDA

View Full Version : Match the text beetween two string



dreamer
20th May 2008, 13:34
I have a text file and i'd like to import just the characters beetween two string:

#####START TEXT id1
Some Text.........bla bla bla.
Bla bla bla.
Bla bla bla.
#####END TEXT id1

some other text
some other text
some other text

#####START TEXT id2
Some Text.........bla bla bla.
Bla bla bla.
Bla bla bla.
#####END TEXT id2

The parser has to print, the text beetween #####START... and ######END.... and also has to know the number id(that is an integer)

How can i obtain this behaviour? Perhaps i guess with QRegExp.....but how????

calhal
20th May 2008, 14:00
You can simply read line by line to QString, then use QString::contains() to check if it contains #####START TEXT or #####END TEXT
Taking out id number from end of QString is simple too i think.

dreamer
20th May 2008, 14:33
Taking out id number from end of QString is simple too i think.


How can i take it?
I don't know if it has one(0-9) or two(0-99) or three digits(0-999) digits.........i guess in this case, i have to use a regexp..........

mazurekwrc
20th May 2008, 14:45
after you get line that contain "#####START TEXT id1"
use remove( "#####START TEXT id" )

wysota
20th May 2008, 14:48
There are many ways to do it. QString::toInt() can convert a string to a number. You can use QString::lastIndexOf() or similar to find position of the last whitespace in the line and treat the rest as the number. Or you can use a regular expression.


QRegExp rx("#####START TEXT (\d+)");
while(file.canReadLine()){
QString line = file.readLine();
if(rx.exactMatch(line)){
qDebug() << "STARTING TAG WITH ID" << rx.cap(1);
}
}

Besides, you know how many characters the "preamble" has (16), so you can read the id directly from the position it should start on.


int id = line.mid(16).toInt();