Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • whitespace
  • tab
  • CR
  • LF

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.

like image 875
user129186 Avatar asked Apr 16 '16 17:04

user129186


1 Answers

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);

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.

like image 136
Wiktor Stribiżew Avatar answered Nov 12 '22 16:11

Wiktor Stribiżew