c++regexqtsplitqstring

Qt - splitting a QString, using several types of whitespace as separators


I'm looking to split a QString. There are several words in the QString, separated by one or more(!) of the following symbols:

I'm looking to extract the words only. Basically, I'm trying to replicate the behavior of the Python str.split() function.

I know I can use a regular expression in order to achieve this, but what would it look like? Any other straightforward methods to achieve this are welcome as well.


Solution

  • Note that CR, LF and tab are already whitespace. If you need to match a whitespace you can rely on a shorthand character class \s:

    \s Matches a whitespace character (QChar::isSpace()).

    So, use then

    QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
    

    Or for Qt6.0:

    str.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
    

    If you plan to split a string with specific characters, use a character class.

    [...] Sets of characters can be represented in square brackets, similar to full regexps. Within the character class, like outside, backslash has no special meaning.

    Then, try

    QStringList list = str.split(QRegExp("[\r\n\t ]+"), QString::SkipEmptyParts);
    

    You can enlarge the list later when requirements change.