如何在 QT 框架工作C++中使用变量作为正则表达式模式
How can use a variable as a regex pattern in QT frame work C++
我想使用字符串变量作为正则表达式模式。但是代码找不到匹配项:
QString pattern, m;
QRegularExpression re (pattern);
QRegularExpressionMatch match;
QRegularExpressionMatchIterator i;
void MainWindow::on_pushButton_clicked()
{
pattern = ui->lineEdit->text();
k = re.globalMatch(ui->lineEdit_2->text());
while (i.hasNext()) {
match = i.next();
m = m +" "+ match.captured(0);
}
ui->label>setText(n);
}
请参阅下面的评论
QString pattern, m; // Construct an empty 'pattern'
QRegularExpression re (pattern); // Construct 're' from that empty pattern
QRegularExpressionMatch match;
QRegularExpressionMatchIterator i;
void MainWindow::on_pushButton_clicked()
{
pattern = ui->lineEdit->text(); // Change 'pattern' - won't have any
// effect on 're' of course.
k = re.globalMatch(ui->lineEdit_2->text()); // Use the original 're'
while (i.hasNext()) {
match = i.next();
m = m +" "+ match.captured(0);
}
ui->label>setText(n);
}
您必须在更改pattern
后构建re
:
QString pattern = ui->lineEdit->text(); // Initialize pattern.
QRegularExpression re(pattern); // Construct 're' from 'pattern'
k = re.globalMatch ...
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 简化C++包括变量名称和函数输出的模式
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- C++ 是否可以使用对变量的引用来简化这种嵌套循环模式?
- 使用静态和避免单例模式声明全局变量
- 如何在 QT 框架工作C++中使用变量作为正则表达式模式
- C++ 静态变量在发布模式下初始化错误
- 可互换声明的变量具有相同的内存地址模式
- 如何使用 XCode 6.1 C++调试模式检查所有局部变量
- (初学者模式)调用时变量的属性
- 模式名称,将值作为静态变量存储在函数中
- 使用循环和变量模式(序列)
- opencvc++模板/模式匹配缩放和旋转不变量
- 为什么我得到两个不同的结果,而使用sregex_iterator与regex变量模式vs构造模式
- 告诉子类对超类的受保护变量执行某些操作是否是一种好的做法(也许是一些已知的设计模式?)?
- 通过变量在fstream trunk和应用程序模式之间切换
- 在调试模式下查看变量的内容?(断点除外)