11

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.

user129186
  • 1,156
  • 2
  • 14
  • 30

1 Answers1

12

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563