Results 1 to 8 of 8

Thread: Split strings using QStringList::split() but ignore quotes.

  1. #1
    Join Date
    Mar 2010
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Split strings using QStringList::split() but ignore quotes.

    I need to split a QStringList but ignore anything inside quotes ("")

    Here is my code:
    Qt Code:
    1. str = cmd.split(QRegExp(" ")); // TODO: Ignore spaces (" ") inside quotes ("")
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help

  2. #2
    Join Date
    Mar 2010
    Posts
    11
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Split strings using QStringList::split() but ignore quotes.

    First use QString::remove() on your string "cmd" to remove the spaces or whatever. Then use QString::split().

  3. #3
    Join Date
    Mar 2010
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Split strings using QStringList::split() but ignore quotes.

    How should that code be:

    When try this:
    Qt Code:
    1. str = cmd.remove(QRegExp("\\s+")).split(" "); // TODO: Ignore spaces (" ") inside quotes ("")
    To copy to clipboard, switch view to plain text mode 

    I get this error: /home/halvors/Prosjekter/openrcon/openrcon-build-desktop/src/../../trunk/src/BFBC2Connection.cpp:100: error: passing ‘const QString’ as ‘this’ argument of ‘QString& QString::remove(const QRegExp&)’ discards qualifiers

  4. #4
    Join Date
    Mar 2010
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Split strings using QStringList::split() but ignore quotes.

    No ideas? Can someone please try svolve this

  5. #5
    Join Date
    Feb 2011
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Split strings using QStringList::split() but ignore quotes.

    You can try this:


    QStringList tmpList = cmd.split(QRegExp("\"")); // Split by "
    bool inside = false;
    list.clear();
    foreach (QString s, tmpList) {
    if (inside) { // If 's' is inside quotes ...
    list.append(s); // ... get the whole string
    } else { // If 's' is outside quotes ...
    list.append(s.split(QRegExp("\\s+"), QString::SkipEmptyParts)); // ... get the splitted string
    }
    inside = !inside;
    }

  6. The following user says thank you to CamiCa for this useful post:

    Phlucious (14th February 2012)

  7. #6
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Split strings using QStringList::split() but ignore quotes.

    This is a great post, and I just used it in my application.

    However, you need to account for the case where the CSV string begins with a quote. A couple small modifications will accomplish this:
    Qt Code:
    1. bool inside = (cmd.at(0) == "\""); //true if the first character is "
    2. QStringList tmpList = cmd.split(QRegExp("\""), QString::SkipEmptyParts); // Split by " and make sure you don't have an empty string at the beginning
    3. QStringList csvlist;
    4. foreach (QString s, tmpList) {
    5. if (inside) { // If 's' is inside quotes ...
    6. csvlist.append(s); // ... get the whole string
    7. } else { // If 's' is outside quotes ...
    8. csvlist.append(s.split(" ", QString::SkipEmptyParts)); // ... get the splitted string
    9. }
    10. inside = !inside;
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2017
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Split strings using QStringList::split() but ignore quotes.

    does the case will be the same if I want to ignore the space after comma , and split only with whitespace which has no comma before it?

  9. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Split strings using QStringList::split() but ignore quotes.

    This is what I use to parse CSV data. If you're not very familiar with regular expressions, it will make your head explode... If you are familiar, it will just give you a migraine!

    Qt Code:
    1. QStringList TableSync::parseCsvFields(const QString &line, const QChar delimiter)
    2. {
    3. QString temp = line;
    4. QString field;
    5. QStringList field_list;
    6.  
    7. // regex explaination
    8. //
    9. // /(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)/g
    10. // (?:^|,) Non-capturing group
    11. // 1st Alternative: ^
    12. // ^ assert position at start of the string
    13. // 2nd Alternative: ,
    14. // , matches the character , literally
    15. // 1st Capturing group (\"(?:[^\"]+|\"\")*\"|[^,]*)
    16. // 1st Alternative: \"(?:[^\"]+|\"\")*\"
    17. // \" matches the character " literally
    18. // (?:[^\"]+|\"\")* Non-capturing group
    19. // Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    20. // 1st Alternative: [^\"]+
    21. // [^\"]+ match a single character not present in the list below
    22. // Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    23. // \" matches the character " literally
    24. // 2nd Alternative: \"\"
    25. // \" matches the character " literally
    26. // \" matches the character " literally
    27. // \" matches the character " literally
    28. // 2nd Alternative: [^,]*
    29. // [^,]* match a single character not present in the list below
    30. // Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    31. // , the literal character ,
    32. // g modifier: global. All matches (don't return on first match)
    33. //
    34.  
    35. QString regex = "(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)";
    36. regex.replace(",", delimiter);
    37.  
    38. QRegularExpression re(regex);
    39.  
    40. if (temp.right(1) == "\n") temp.chop(1);
    41.  
    42. QRegularExpressionMatchIterator it = re.globalMatch(temp);
    43.  
    44. while (it.hasNext())
    45. {
    46. QRegularExpressionMatch match = it.next();
    47. if (match.hasMatch())
    48. {
    49. field = match.captured(1);
    50. if (field.left(1) == "\"" && field.right(1) == "\"")
    51. field = field.mid(1, field.length()-2);
    52. field_list.push_back(field);
    53. }
    54. }
    55.  
    56. return field_list;
    57. }
    To copy to clipboard, switch view to plain text mode 
    Hope that helps.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. split QByteArray
    By xproch13 in forum Newbie
    Replies: 2
    Last Post: 29th October 2010, 22:50
  2. split a QTabWidget?
    By shud in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2009, 20:56
  3. how to split a class ?
    By mimmo_kallon in forum Newbie
    Replies: 0
    Last Post: 8th April 2008, 12:37
  4. More appropriate, section or split?
    By ucntcme in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2007, 13:10
  5. QString split()
    By ShaChris23 in forum Newbie
    Replies: 4
    Last Post: 3rd May 2007, 05:10

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.