Results 1 to 4 of 4

Thread: simple QRegExp

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default simple QRegExp

    if I have:

    "My name is @Pinco Pallino@ and your name is @Panco Pallone@..."

    I want to extract 'Pinco Pallino' and 'Panco Pallone'

    I think that is possible with the regular expression QRegExp ...how can I do this???

    P.S. I could have: " @Pinco Pallino@ is my name ... " (it could be n match words between @ )

    Thanks

    P.S. 2 : I've try with:

    Qt Code:
    1. QString str ="My name is @Pinco Pallino@ and your name is @Panco Pallone@";
    2. QRegExp regex("@*@");
    3. regex.setPatternSyntax(QRegExp::Wildcard);
    4. regex.indexIn(str);
    5.  
    6. QStringList list = regex.capturedTexts();
    To copy to clipboard, switch view to plain text mode 

    the result is: "@Pinco Pallino@ and your name is @Panco Pallone@"...Instead I want:

    @Pinco Pallino@
    @Panco Pallone@

  2. #2
    Join Date
    Sep 2008
    Posts
    54
    Thanks
    3
    Thanked 10 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: simple QRegExp

    Paolom,

    Look at:

    Qt Code:
    1. void QRegExp::setMinimal ( bool minimal )
    To copy to clipboard, switch view to plain text mode 

    and with the result you get you might want to look at:

    Qt Code:
    1. QString QRegExp::cap ( int nth = 0 ) const
    To copy to clipboard, switch view to plain text mode 

    Regards,

    marcel

  3. #3
    Join Date
    Oct 2009
    Posts
    70

    Default Re: simple QRegExp

    Thanks, this is my final working code ( i hope! )\:

    Qt Code:
    1. // str contains the string to match
    2.  
    3. QRegExp regex("@.*@");
    4. regex.setMinimal(true);
    5.  
    6. int pos = 0;
    7.  
    8. while ((pos = regex.indexIn(str, pos)) != -1)
    9. {
    10. list << regex.cap(0).remove(QChar('@'),Qt::CaseInsensitive);
    11. pos += regex.matchedLength();
    12. }
    13.  
    14. return list;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: simple QRegExp

    I would go for a pure regular expression like:
    @([^@]+)@
    or if you want the @ also:
    (@[^@]+@)

Similar Threads

  1. QRegExp
    By tebessum in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2008, 18:44
  2. QRegExp?
    By Marco812 in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2006, 08:31
  3. need help for my QRegExp
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2006, 16:29
  4. QRegExp
    By evgenM in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2006, 14:22
  5. Trouble parsing using simple QRegExp
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2006, 00:42

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.