c++ - QString::replace(const QRegExp &, const QString &) 和 QString::replace(const QRegularExpression &, const QString &) 工作方式不同

标签 c++ qt qstring

为什么 Qt 5.1.0 Release Candidate 函数 QString::replace(const QRegExp & rx, const QString & after) 以某种方式处理\v 而 QString::replace(const QRegularExpression & re, const QString & after) ) 用另一种方式? 这就是我使用的代码的和平:

QString ss("a\t\v  bc \t cdef\vg\r\r\t hi");
QString ss1(ss);
ss1.replace(QRegExp("\\s{2,}"), " ");
QString ss2(ss);
ss2.replace(QRegularExpression("\\s{2,}"), " ");

调试器的值是:

ss  "a\t\013  bc \t cdef\013g\r\r\t hi"
ss1 "a bc cdef\013g hi"
ss2 "a\t\013 bc cdef\013g hi"

谢谢

最佳答案

QRegExp 为 \s 使用 Unicode“分隔符”类别。这包括 \v

QRegularExpression 是 PCRE 的包装器,文档指出 (http://pcre.org/pcre.txt):

For compatibility with Perl, \s does not match the VT character (code 11). This makes it different from the the POSIX "space" class. The \s characters are HT (9), LF (10), FF (12), CR (13), and space (32). If "use locale;" is included in a Perl script, \s may match the VT charac- ter. In PCRE, it never does.

尽管文档说它永远不会匹配 \v,您可以尝试将 UseUnicodePropertiesOption 选项传递给 QRegularExpression,它会更改字符类以使用 Unicode 属性,所以理论上,除非 PCRE 内置了特定的异常,否则 \s 应该匹配 \v

否则,您可以使用 (\h|\v)(C++ 字符串形式为 "(\\h|\\v)"),使用PCRE 的特殊“水平空间”和“垂直空间”类。

关于c++ - QString::replace(const QRegExp &, const QString &) 和 QString::replace(const QRegularExpression &, const QString &) 工作方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280005/

相关文章:

c++ - 从 QML 调用 QAbstractTableModel setData 方法

c++ - QT DBUS挂载文件系统

c++ - 在 Qt 中;将 QString 中每个单词的首字母大写的最佳方法是什么?

c++ - 如何捕获从 MFC web 浏览器 c++ 发出的 GET/POST 请求

c++ - 广泛使用OpenGL绘制世界地图[WGS84]

c++ - 使QT表单不显示在任务栏中

c++ - 使用 QXmlStreamWriter 编写 XML 代码

c++ - 将 QString 转换为其他类型

c++ - boost::thread & 成员函数

c++ - "undefined class"Qt/C++