Results 1 to 7 of 7

Thread: Split QString only at unescaped slash

  1. #1
    Join Date
    Jun 2013
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Split QString only at unescaped slash

    Hello!

    Let's assume I have the following QString: "hello/world/how\/are/you".

    I would like to split the string into a QStringList with the following content: {"hello", "world", "how\/are", "you"}. The best success so far I had with
    Qt Code:
    1. QRegExp ex("[^\\\\]/")
    To copy to clipboard, switch view to plain text mode 
    , but that unfortunately cuts off the last letter before the backslash. Is there a way to achive the desired split? BTW, I was only able to split the string if I escaped the backslash in "how\\/are" manually,
    Qt Code:
    1. string.replace("\\", "\\\\")
    To copy to clipboard, switch view to plain text mode 
    did not work, why is that?

    BR

  2. #2
    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 QString only at unescaped slash

    You should consider QRegularExpression over QRegExp. QRegularExpression is compatible with Perl Compatible Regular Expressions (PCRE). The following QRegularExpression works for your test string (I didn't test extensively):
    Qt Code:
    1. QRegularExpression re("(\\w+(?:\\\\\\/)*\\w*)?");
    To copy to clipboard, switch view to plain text mode 
    I didn't compile this, so I hope I didn't screw up the backslash quoting. The raw PCRE is (\w+(?:\\\/)*\w*)? and was tested at the awesome regex test site (another reason to use QRegularExpression over QRegExp):

    https://regex101.com/

    Good Luck

  3. #3
    Join Date
    Jun 2013
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Split QString only at unescaped slash

    Thank you very much for your input. Unfortunately your suggestion does not provide the desired result:

    Qt Code:
    1. ""
    2. ""
    3. "/"
    4. ""
    5. "/"
    6. ""
    7. "/"
    8. ""
    9. "/"
    10. ""
    11. ""
    To copy to clipboard, switch view to plain text mode 

    Is it possible to logically concatenate regular expressions? Something like
    Qt Code:
    1. "/" AND NOT "\/"
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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 QString only at unescaped slash

    Sorry, the first regex worked with QRegularExpression::globalMatch, try this one with QString::split:
    Qt Code:
    1. QString str = "hello/world/how\\/are/you";
    2. QRegularExpression re("\\/(?<!\\\\\\/)");
    3. QStringList strlist = str.split(re);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: Split QString only at unescaped slash

    You are looking for a negative look-behind assertion. The bare regular expression looks like this:
    Qt Code:
    1. (?<!\\)\/
    To copy to clipboard, switch view to plain text mode 
    when used on jthomps testing web site and matches only the slashes you wish to split on. In C++ code that would look like this for QRegularExpression:
    Qt Code:
    1. QRegularExpression re("(?<!\\\\)/");
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2013
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Split QString only at unescaped slash

    Thank you again, that works, but only if the backslash is already escaped. A user who enters the string will use the backslash as an escape character, unaware of the fact, that Qt requires a double backslash. I was trying to replace the single backslash with
    Qt Code:
    1. string.replace("\\", "\\\\");
    To copy to clipboard, switch view to plain text mode 
    but that did not work.
    Qt Code:
    1. QRegExp::escape()
    To copy to clipboard, switch view to plain text mode 
    did not work either. By parsing the string character for character I realized the backslash is not even recognized. That is too bad. Is there a way around this?

  7. #7
    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 QString only at unescaped slash

    Qt doesn't require double backslashes, it's the C++ compiler that requires the double backslashes in quoted strings. If you are getting the string from user input or reading from a file, double backslashes are not required. This example reads the string from the file:
    Qt Code:
    1. QFile f("/Users/jefft/test.txt");
    2. f.open(QFile::ReadOnly);
    3. QString str = f.readLine();
    4. f.close();
    5. QRegularExpression re("\\/(?<!\\\\\\/)");
    6. QStringList strlist = str.split(re);
    To copy to clipboard, switch view to plain text mode 
    Where the test.txt file contains:

    hello/world/how\/are/you

    This should be the same if your text is coming from a QLineEdit, etc.

  8. The following user says thank you to jefftee for this useful post:

    QphiuchuS (20th January 2015)

Similar Threads

  1. Replies: 7
    Last Post: 6th February 2017, 20:10
  2. Replies: 1
    Last Post: 28th April 2013, 11:47
  3. Replies: 5
    Last Post: 26th January 2012, 15:04
  4. Replies: 2
    Last Post: 9th January 2009, 20:49
  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.