Results 1 to 5 of 5

Thread: Match the text beetween two string

  1. #1
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question 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????

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Match the text beetween two string

    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..........

  4. #4
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Match the text beetween two string

    after you get line that contain "#####START TEXT id1"
    use remove( "#####START TEXT id" )

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

    Qt Code:
    1. QRegExp rx("#####START TEXT (\d+)");
    2. while(file.canReadLine()){
    3. QString line = file.readLine();
    4. if(rx.exactMatch(line)){
    5. qDebug() << "STARTING TAG WITH ID" << rx.cap(1);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. int id = line.mid(16).toInt();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. read input string --> text mode
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2007, 19:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.