【问题标题】:QRegExp does not operate as expectedQRegExp 未按预期运行
【发布时间】:2017-01-18 02:21:58
【问题描述】:

我有一个 QString "mystring",它是从这个 shell 命令的返回值中捕获的:

    df /storage/sdcard0/ 

捕获的字符串是:

   Filesystem               Size     Used     Free   Blksize
   /storage/sdcard0/        5.5G     3.9G     1.6G   4096

我将 QString 转换为 QStringList :

   QStringList list=mystring.split(QRegExp("\\s"));
   int i = list.count();

但是,当我检查列表时:

  for( int a = 0; a < i; a = a + 1 )
             qDebug() << list[a];

"Filesystem"
""
"Size"
""
"Used"
""
"Free"
""
"Blksize"
""
"/sdcard/"
""
"5.5G"
""
"3.9G"
""
"1.6G"
""
"4096"
""
""

为简洁起见,我省略了一堆“”。如果我使用相同的拆分 输入的字符串它工作得很好。

       QString mystring = "This is a sentence with words"

qDebug 显示:

"This"
"is"
"a"
"sentence"
"with"
"words"

如何阻止这些“”被添加到 QStringList 中?这可以用不同的 QRegExp 纠正吗?

【问题讨论】:

  • 对不起:QStringList list=mystring.split(QRegExp("\\s"),QString::SkipEmptyParts);
  • 工作,谢谢!如果您添加为答案,我会接受它已解决。

标签: c++ qt split qregexp


【解决方案1】:

查看QString::split(const QRegExp &amp;rx, SplitBehavior behavior = KeepEmptyParts) const 有一个可选参数SplitBehavior 允许您指定如何处理空部分。如果您保留默认的 QString::split() 将保留空白部分。要跳过,您需要将其设置为 QString::SkipEmptyParts

因此,将拆分更改为以下内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2020-06-28
    • 2012-02-18
    • 2018-01-18
    • 2012-06-14
    • 2019-03-03
    • 2012-09-21
    相关资源
    最近更新 更多