QRegularExpression example: Handle empty patterns
Previously, when clearing the pattern by clicking the clear button, the result match list would display a list of empty matches since apparently an empty pattern matches as many times as the subject is long. This is confusing when using the tool. Restructure RegularExpressionDialog::refresh() so that it bails out if the pattern is empty or invalid. Change-Id: I8119a75db50cead3f64394016e3390a9bf7d0bf7 Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
This commit is contained in:
parent
c1e7d0795b
commit
ee8b61bcf5
@ -109,6 +109,19 @@ RegularExpressionDialog::RegularExpressionDialog(QWidget *parent)
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RegularExpressionDialog::setResultUiEnabled(bool enabled)
|
||||||
|
{
|
||||||
|
matchDetailsTreeWidget->setEnabled(enabled);
|
||||||
|
namedGroupsTreeWidget->setEnabled(enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setTextColor(QWidget *widget, const QColor &color)
|
||||||
|
{
|
||||||
|
QPalette palette = widget->palette();
|
||||||
|
palette.setColor(QPalette::Text, color);
|
||||||
|
widget->setPalette(palette);
|
||||||
|
}
|
||||||
|
|
||||||
void RegularExpressionDialog::refresh()
|
void RegularExpressionDialog::refresh()
|
||||||
{
|
{
|
||||||
setUpdatesEnabled(false);
|
setUpdatesEnabled(false);
|
||||||
@ -125,7 +138,30 @@ void RegularExpressionDialog::refresh()
|
|||||||
escaped.append(QLatin1Char('"'));
|
escaped.append(QLatin1Char('"'));
|
||||||
escapedPatternLineEdit->setText(escaped);
|
escapedPatternLineEdit->setText(escaped);
|
||||||
|
|
||||||
|
setTextColor(patternLineEdit, subjectTextEdit->palette().color(QPalette::Text));
|
||||||
|
matchDetailsTreeWidget->clear();
|
||||||
|
namedGroupsTreeWidget->clear();
|
||||||
|
regexpStatusLabel->setText(QString());
|
||||||
|
|
||||||
|
if (pattern.isEmpty()) {
|
||||||
|
setResultUiEnabled(false);
|
||||||
|
setUpdatesEnabled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QRegularExpression rx(pattern);
|
QRegularExpression rx(pattern);
|
||||||
|
if (!rx.isValid()) {
|
||||||
|
setTextColor(patternLineEdit, Qt::red);
|
||||||
|
regexpStatusLabel->setText(tr("Invalid: syntax error at position %1 (%2)")
|
||||||
|
.arg(rx.patternErrorOffset())
|
||||||
|
.arg(rx.errorString()));
|
||||||
|
setResultUiEnabled(false);
|
||||||
|
setUpdatesEnabled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setResultUiEnabled(true);
|
||||||
|
|
||||||
QRegularExpression::MatchType matchType = matchTypeComboBox->currentData().value<QRegularExpression::MatchType>();
|
QRegularExpression::MatchType matchType = matchTypeComboBox->currentData().value<QRegularExpression::MatchType>();
|
||||||
QRegularExpression::PatternOptions patternOptions = QRegularExpression::NoPatternOption;
|
QRegularExpression::PatternOptions patternOptions = QRegularExpression::NoPatternOption;
|
||||||
QRegularExpression::MatchOptions matchOptions = QRegularExpression::NoMatchOption;
|
QRegularExpression::MatchOptions matchOptions = QRegularExpression::NoMatchOption;
|
||||||
@ -156,60 +192,40 @@ void RegularExpressionDialog::refresh()
|
|||||||
|
|
||||||
rx.setPatternOptions(patternOptions);
|
rx.setPatternOptions(patternOptions);
|
||||||
|
|
||||||
QPalette palette = patternLineEdit->palette();
|
const int capturingGroupsCount = rx.captureCount() + 1;
|
||||||
if (rx.isValid())
|
|
||||||
palette.setColor(QPalette::Text, subjectTextEdit->palette().color(QPalette::Text));
|
|
||||||
else
|
|
||||||
palette.setColor(QPalette::Text, Qt::red);
|
|
||||||
patternLineEdit->setPalette(palette);
|
|
||||||
|
|
||||||
matchDetailsTreeWidget->clear();
|
QRegularExpressionMatchIterator iterator = rx.globalMatch(text, offsetSpinBox->value(), matchType, matchOptions);
|
||||||
matchDetailsTreeWidget->setEnabled(rx.isValid());
|
int i = 0;
|
||||||
|
|
||||||
if (rx.isValid()) {
|
while (iterator.hasNext()) {
|
||||||
const int capturingGroupsCount = rx.captureCount() + 1;
|
QRegularExpressionMatch match = iterator.next();
|
||||||
|
|
||||||
QRegularExpressionMatchIterator iterator = rx.globalMatch(text, offsetSpinBox->value(), matchType, matchOptions);
|
QTreeWidgetItem *matchDetailTopItem = new QTreeWidgetItem(matchDetailsTreeWidget);
|
||||||
int i = 0;
|
matchDetailTopItem->setText(0, QString::number(i));
|
||||||
|
|
||||||
while (iterator.hasNext()) {
|
for (int captureGroupIndex = 0; captureGroupIndex < capturingGroupsCount; ++captureGroupIndex) {
|
||||||
QRegularExpressionMatch match = iterator.next();
|
QTreeWidgetItem *matchDetailItem = new QTreeWidgetItem(matchDetailTopItem);
|
||||||
|
matchDetailItem->setText(1, QString::number(captureGroupIndex));
|
||||||
QTreeWidgetItem *matchDetailTopItem = new QTreeWidgetItem(matchDetailsTreeWidget);
|
matchDetailItem->setText(2, match.captured(captureGroupIndex));
|
||||||
matchDetailTopItem->setText(0, QString::number(i));
|
|
||||||
|
|
||||||
for (int captureGroupIndex = 0; captureGroupIndex < capturingGroupsCount; ++captureGroupIndex) {
|
|
||||||
QTreeWidgetItem *matchDetailItem = new QTreeWidgetItem(matchDetailTopItem);
|
|
||||||
matchDetailItem->setText(1, QString::number(captureGroupIndex));
|
|
||||||
matchDetailItem->setText(2, match.captured(captureGroupIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
matchDetailsTreeWidget->expandAll();
|
matchDetailsTreeWidget->expandAll();
|
||||||
|
|
||||||
namedGroupsTreeWidget->clear();
|
regexpStatusLabel->setText(tr("Valid"));
|
||||||
namedGroupsTreeWidget->setEnabled(rx.isValid());
|
|
||||||
|
|
||||||
if (rx.isValid()) {
|
const QStringList namedCaptureGroups = rx.namedCaptureGroups();
|
||||||
regexpStatusLabel->setText(tr("Valid"));
|
for (int i = 0; i < namedCaptureGroups.size(); ++i) {
|
||||||
|
const QString currentNamedCaptureGroup = namedCaptureGroups.at(i);
|
||||||
|
|
||||||
const QStringList namedCaptureGroups = rx.namedCaptureGroups();
|
QTreeWidgetItem *namedGroupItem = new QTreeWidgetItem(namedGroupsTreeWidget);
|
||||||
for (int i = 0; i < namedCaptureGroups.size(); ++i) {
|
namedGroupItem->setText(0, QString::number(i));
|
||||||
const QString currentNamedCaptureGroup = namedCaptureGroups.at(i);
|
namedGroupItem->setText(1, currentNamedCaptureGroup.isNull() ? tr("<no name>") : currentNamedCaptureGroup);
|
||||||
|
|
||||||
QTreeWidgetItem *namedGroupItem = new QTreeWidgetItem(namedGroupsTreeWidget);
|
|
||||||
namedGroupItem->setText(0, QString::number(i));
|
|
||||||
namedGroupItem->setText(1, currentNamedCaptureGroup.isNull() ? tr("<no name>") : currentNamedCaptureGroup);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
regexpStatusLabel->setText(tr("Invalid: syntax error at position %1 (%2)")
|
|
||||||
.arg(rx.patternErrorOffset())
|
|
||||||
.arg(rx.errorString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setUpdatesEnabled(true);
|
setUpdatesEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +78,7 @@ private:
|
|||||||
void setupUi();
|
void setupUi();
|
||||||
QWidget *setupLeftUi();
|
QWidget *setupLeftUi();
|
||||||
QWidget *setupRightUi();
|
QWidget *setupRightUi();
|
||||||
|
void setResultUiEnabled(bool enabled);
|
||||||
|
|
||||||
QLineEdit *patternLineEdit;
|
QLineEdit *patternLineEdit;
|
||||||
QLineEdit *escapedPatternLineEdit;
|
QLineEdit *escapedPatternLineEdit;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user