Results 1 to 4 of 4

Thread: Separate the words by QRegex

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Separate the words by QRegex

    Qt Code:
    1. QString contents = "lll-###";
    2.  
    3. QRegExp split("\\b");
    4.  
    5. QStringList splitContents = contents.split(split, QString::SkipEmptyParts);
    6. for(QString const &data : splitContents)
    7. {
    8. qDebug() << data;
    9. }
    To copy to clipboard, switch view to plain text mode 

    What I want is "lll-", "###"
    The result is "lll", "-###"

    What should I do?Thanks

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Separate the words by QRegex

    1) work around it.
    2) use a different method (string.split ?)
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Separate the words by QRegex

    1) work around it.
    My pretty nasty work around

    Qt Code:
    1. //helper function
    2. std::list<std::pair<int, int> > get_positions(QRegExp const &rx, QString const &contents)
    3. {
    4. int pos = 0;
    5. std::list<std::pair<int, int> > result;
    6. while((pos = rx.indexIn(contents, pos)) != -1)
    7. {
    8. //qDebug() << pos2 << ", "<< rx.matchedLength();
    9. result.push_back(std::make_pair(pos, rx.matchedLength()));
    10. pos += rx.matchedLength();
    11. }
    12.  
    13. return result;
    14. }
    15.  
    16. //helper function
    17. template<typename T>
    18. QStringList const split_str_by_positions(T const &position, QString const &contents)
    19. {
    20. QStringList results;
    21. for(std::pair<int, int> const &data : position)
    22. {
    23. results << contents.mid(data.first, data.second);
    24. }
    25.  
    26. return results;
    27. }
    28.  
    29. //split the contents into the QStringList
    30. void test_get_positions()
    31. {
    32. QString contents = "lll ### ### ###";
    33.  
    34. typedef std::pair<int, int> DInt;
    35. std::list<DInt> positions = get_positions(QRegExp("#+"), contents);
    36.  
    37. std::list<DInt> positions2 = get_positions(QRegExp("[^#]+"), contents);
    38.  
    39. struct DIntCompare
    40. {
    41. bool operator()(DInt const &one, DInt const &two) const
    42. { return one.first < two.first; }
    43. };
    44.  
    45. positions.merge(positions2, DIntCompare());
    46.  
    47. qDebug() << "---------------------";
    48.  
    49. for(DInt const &data : positions) qDebug() << data.first <<", "<<data.second;
    50.  
    51. QStringList results = split_str_by_positions(positions, contents);
    52.  
    53. for(QString const &data : results) qDebug() << data;
    54. }
    To copy to clipboard, switch view to plain text mode 

    Do you have other easier solutions?
    My workaround is ugly, want to find other better way to implement it.
    Thanks
    Last edited by stereoMatching; 25th July 2012 at 12:58.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Separate the words by QRegex

    Two other ways:
    Qt Code:
    1. QString contents = "lll-###";
    2.  
    3. QStringList result;
    4. int index = contents.lastIndexOf('-');
    5. result << contents.left(index + 1) << contents.mid(index + 1);
    6. qDebug() << result;
    7.  
    8.  
    9. result.clear();
    10. QRegExp re("(.+-)(#+)");
    11. if (re.indexIn(contents) >= 0)
    12. result << re.cap(1) << re.cap(2);
    13. qDebug() << result;
    To copy to clipboard, switch view to plain text mode 
    This is where you tell us that this was not really the requirement you had in mind.

Similar Threads

  1. Khmer language words sorting
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2011, 12:16
  2. Replies: 1
    Last Post: 24th August 2011, 13:49
  3. Highlighting individual words of a sentence
    By kellorooney in forum Newbie
    Replies: 1
    Last Post: 16th April 2010, 04:51
  4. Adding 3 words to Button with specified length betwwen words
    By chikkireddi in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 11:08
  5. how to color certain words in a textedit
    By chandrabose.s in forum Newbie
    Replies: 12
    Last Post: 15th March 2006, 09:24

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.