Results 1 to 17 of 17

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

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

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

    My case:
    I hava a file: a.txt

    a e-mail address is in the file.

    how can i get the e-mail String??

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

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

    J-P Nurmi

  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)

    Now , I find a: bool QString::contains(const QRegExp & rx ) const

    but it is return a bool value~ I need QStrings

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    But with the classes Jpn suggested you can read QStrings from a file.
    Read the docs to find out how you can do that.

    Regards

  5. #5
    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)

    no Class QString is not supply any method to get it.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Are you sure?
    What about this:
    Qt Code:
    1. QFile file("somefile.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4. QTextStream textStream( &file);
    5. QString fileContents = textStream.readAll();
    To copy to clipboard, switch view to plain text mode 

    You can also read the stream in chunks, if the file is too big.

    Regards

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

    And here is a nice RegEx for email matching:
    http://www.regular-expressions.info/email.html
    ==========================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.

  8. #8
    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)

    yes , I can ,I can read the file now,

    now , this string is a big string, a e-mail address in the string ,

    I want get the e-mail address use regular expressions.. How TO

  9. #9
    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)

    READ my last post.
    ==========================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.

  10. #10
    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);
    qDebug() << str.count(reg); // output: 3 OK


    reg.indexIn(str);
    reg.cap(0); // output: "bob" OK
    reg.cap(1); // output: "" why
    reg.cap(2); // output: "" why

  11. #11
    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)

    Because I think you missunderstood the docs:
    Each subsequent element corresponds to the next capturing open left parentheses. Thus cap(1) is the text of the first capturing parentheses, cap(2) is the text of the second, and so on.
    So QRegEx::cap(int n) does not retrieve the n'th match, but the match of the n'th parentheses in your expression, and you have only one (n=0) which is why only the first cap() returns a non empty string.

    What you are looking for is QRegExp::capturedTexts() I think.
    ==========================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.

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

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

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

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

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

  17. #17
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.