可以使用以下正则表达式来查找{}内的数据:
\{([^{}]+)\}
这个正则表达式会匹配一个左花括号,然后匹配任意数量的非花括号字符,并将它们捕获到组中,最后匹配右花括号。由于只允许一层嵌套,所以不需要考虑嵌套情况。
下面是一个示例代码,演示如何使用QRegExp在字符串中查找{}内的数据:
#include <QRegExp>
#include <QString>
#include <QDebug>
int main()
{
QString str = "first {second} third {fourth} fifth";
QRegExp rx("\\{([^{}]+)\\}");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
QString match = rx.cap(1);
qDebug() << match;
pos += rx.matchedLength();
}
return 0;
}
输出结果为:
"second"
"fourth"