Results 1 to 17 of 17

Thread: How to get a QString from a file (use regular expression)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to get a QString from a file (use regular expression)

    I try it (QRegExp::capturedTexts() ) ,

    find one ....

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to get a QString from a file (use regular expression)

    what do you mean?
    in your last post count() returned 3...
    Better if you show your code, and not only explain the end effect.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to get a QString from a file (use regular expression)

    QString str="my name is bob,bobis good boy,i lick bob<";
    QRegExp reg("bob");
    reg.indexIn(str);
    QStringList list=reg.capturedTexts();
    qDebug() << list.count(); // output : 1

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to get a QString from a file (use regular expression)

    After a closer reading, it turns out capturedTexts() is not the right method you are looking for:
    Some regexps can match an indeterminate number of times. For example if the input string is "Offsets: 12 14 99 231 7" and the regexp, rx, is (\d+)+, we would hope to get a list of all the numbers matched. However, after calling rx.indexIn(str), capturedTexts() will return the list ("12", "12"), i.e. the entire match was "12" and the first subexpression matched was "12". The correct approach is to use cap() in a loop.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Exclamation Re: How to get a QString from a file (use regular expression)

    I'm read the Qt Assistant yesterday, I thinks should be :

    Qt Code:
    1. QString str="bob's msn is aa@g.com,and bob's email is qq@aaaa.com.cn :)";
    2. QRegExp reg("[A-z0-9_\.-]+@[A-z0-9\.]+");
    3.  
    4. int count=str.count(reg);
    5. int from=0;
    6. int in=0;
    7.  
    8. for(int i=0;i<count;i++)
    9. {
    10. in=reg.indexIn(str,from);
    11. rs.append(reg.cap(0));
    12. from=in+rs.value(i).size();
    13. }
    14.  
    15.  
    16. qDebug() << rs.count(); //output 4(aa@g.com,a@g.com,qq@aaaa.com.cn,q@aaaa.com.cn)
    17.  
    18. qDebug() << rs.value(0); //output "aa@g.com"
    19. qDebug() << rs.value(1); //output "qq@aaaa.com.cn"
    20. qDebug() << rs.value(2); //output ""
    21. qDebug() << rs.value(3); //output ""
    To copy to clipboard, switch view to plain text mode 


    how about this is that ok?

    and please check my regular expressions"[A-z0-9_\.-]+@[A-z0-9\.]+".
    Last edited by fengtian.we; 31st May 2007 at 10:40.

  6. #6
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to get a QString from a file (use regular expression)

    OK!!!! Thank you everyone!

    this is my way:


    Qt Code:
    1. QString str="bob's msn is aaa@g.com,and bob's email is qq@aaaa.com.cn :)";
    2. QRegExp reg("[A-z0-9_\.-]+@[A-z0-9\.]+");
    3.  
    4. int startPos=0;
    5. int mailPos=0;
    6.  
    7. while(true)
    8. {
    9. mailPos=reg.indexIn(str,startPos);
    10. if(mailPos<0)
    11. {
    12. break;
    13. }
    14. rs.append(reg.cap(0));
    15. startPos=mailPos+rs.last().size();
    16. }
    17.  
    18. qDebug() << rs.count(); // 2
    19. qDebug() << rs.value(0); // "aaa@g.com"
    20. qDebug() << rs.value(1); // "qq@aaaa.com.cn"
    To copy to clipboard, switch view to plain text mode 

    haha

Similar Threads

  1. Regular Expression for QDate [YYYY/MM/DD] format
    By bera82 in forum Qt Programming
    Replies: 6
    Last Post: 3rd August 2019, 09:40
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  4. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52

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
  •  
Qt is a trademark of The Qt Company.