要获取QLineEdit中输入框(001)中的所有数字(包含0),你可以稍微修改之前给出的代码。将正则表达式改为 \\d*
,即匹配零个或多个数字。以下是修改后的示例代码:
#include <QApplication>
#include <QRegularExpression>
#include <QDebug>
#include <QLineEdit>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QLineEdit lineEdit;
lineEdit.setText("(001)");
QString text = lineEdit.text();
QRegularExpression regex("\\d*"); // 正则表达式匹配零个或多个数字
QRegularExpressionMatchIterator it = regex.globalMatch(text);
QStringList numbers;
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString number = match.captured(0);
if (!number.isEmpty()) {
numbers << number;
}
}
qDebug() << "Numbers: " << numbers;
return app.exec();
}
在上述代码中,我们使用了正则表达式 \\d*
来匹配零个或多个数字,然后在循环中检查捕获到的子字符串是否为空,如果不为空,则将其添加到字符串列表 numbers
中。
这样就可以获取包含0的所有数字了。请根据你的实际需要进行相应的修改。
内容由零声教学AI助手提供,问题来源于学员提问