//---------------------------------------------------------------------------
/*
RegexTester, regular expression tester
Copyright (C) 2010 Richel Bilderbeek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolRegexTester.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <string>
//---------------------------------------------------------------------------
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
//---------------------------------------------------------------------------
#include "dialogabout.h"
#include "dialogmain.h"
#include "ui_dialogmain.h"
//---------------------------------------------------------------------------
DialogMain::DialogMain(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogMain)
{
ui->setupUi(this);
QObject::connect(ui->edit_line,SIGNAL(textEdited(QString)),this,SLOT(onAnyChange()));
QObject::connect(ui->edit_regex,SIGNAL(textEdited(QString)),this,SLOT(onAnyChange()));
QObject::connect(ui->button_about,SIGNAL(clicked()),this,SLOT(onAboutClick()));
ui->edit_line->setText("Both '1234 AB' and '9999 ZZ' are valid Dutch zip codes");
ui->edit_regex->setText("\\d{4}\\s[A-Z]{2}");
this->onAnyChange();
}
//---------------------------------------------------------------------------
DialogMain::~DialogMain()
{
delete ui;
}
//---------------------------------------------------------------------------
void DialogMain::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void DialogMain::onAnyChange()
{
const std::string regex_string = ui->edit_regex->text().toStdString();
//Check if regex input is valid
try
{
const boost::regex regex_temp(regex_string);
}
catch (boost::regex_error& e)
{
ui->label_regex_valid->setText("Regex valid: no");
ui->label_regex_match->setText("Regex matches line: n/a");
ui->label_regex_found->setText("Regex found in line: n/a");
return;
}
ui->label_regex_valid->setText("Regex valid: yes");
const boost::regex r(regex_string);
const std::string s = ui->edit_line->text().toStdString();
ui->label_regex_match->setText("Regex matches line: "
+ QString(boost::regex_match(s,r) ? "yes" : "no"));
ui->edit_matching_regexes->clear();
if (boost::regex_search(s,r))
{
ui->label_regex_found->setText("Regex found in line: yes");
//Show all matches
const std::vector<std::string> v = GetRegexMatches(s,r);
BOOST_FOREACH(const std::string i,v)
{
ui->edit_matching_regexes->appendPlainText(
QString(i.c_str()));
}
}
else
{
ui->label_regex_found->setText("Regex found in line: no");
}
}
//---------------------------------------------------------------------------
void DialogMain::onAboutClick()
{
DialogAbout d;
d.exec();
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppGetRegexMatches.htm
const std::vector<std::string> GetRegexMatches(
const std::string& s,
const boost::regex& r)
{
std::vector<std::string> v;
std::string::const_iterator start = s.begin();
const std::string::const_iterator end = s.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while(boost::regex_search(start, end, what, r, flags))
{
const std::string x = what.str();
v.push_back(x);
if (v.size() == 10) return v;
start = what[0].second;
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
return v;
}
//---------------------------------------------------------------------------
|