How can I use QRegExps to find each item in a list? What I am trying to do is to get each team in a list. They must be separated by a comma or a comma and space. For example, I want to get each team in this list:

    Patriots, Celtics, Red Sox

My initial regular expression was:

    [^, ]+

There is a problem with this because teams are allowed to have space characters in them, such as the Red Sox. Another one I tried was this:

    [^,] +

But that caused problems because it grabbed the space after the comma, and appended it to the beginning of the name of the following item in the list.

Any solutions?

有帮助吗?

解决方案

list = str.split(QRegExp(",\\s+"));

Or, when whitespaces are optional:

list = str.split(QRegExp(",\\s*"));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top