Match the text beetween two string
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????
Re: Match the text beetween two string
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.
Re: Match the text beetween two string
Quote:
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..........
Re: Match the text beetween two string
after you get line that contain "#####START TEXT id1"
use remove( "#####START TEXT id" )
Re: Match the text beetween two string
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.
Code:
QRegExp rx
("#####START TEXT (\d+)");
while(file.canReadLine()){
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.
Code:
int id = line.mid(16).toInt();