Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's tools.

 

 

 

 

 

(Tool) RegexTester source code (version 1.2)

 

RegexTester source code (version 1.2).

 

Operating system(s) or programming environment(s)

 

IDE(s):

Project type:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-08-06T16:34:35
#
#-------------------------------------------------
QT       += core gui
TARGET = ToolRegexTester
TEMPLATE = app
LIBS += -L/usr/local/lib -lboost_regex
SOURCES += main.cpp\
        dialogmain.cpp \
    dialogabout.cpp \
    dialogwhatsnew.cpp
HEADERS  += dialogmain.h \
    dialogabout.h \
    dialogwhatsnew.h
FORMS    += dialogmain.ui \
    dialogabout.ui \
    dialogwhatsnew.ui
RESOURCES += \
    resources.qrc

 

 

 

 

 

dialogabout.cpp

 

//---------------------------------------------------------------------------
/*
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 <algorithm>
#include <string>
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
#include <boost/version.hpp>
//---------------------------------------------------------------------------
#include <Qt/qglobal.h>
//---------------------------------------------------------------------------
#include "dialogabout.h"
#include "dialogmain.h"
#include "dialogwhatsnew.h"
#include "ui_dialogabout.h"
//---------------------------------------------------------------------------
DialogAbout::DialogAbout(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogAbout)
{
  ui->setupUi(this);

  ui->label_title->setText(
    QString("RegexTester version ")
    + QString(DialogMain::GetVersion().c_str()));


  ui->label_boost_version->setText(
    QString("Boost version: ")
    + QString(GetBoostVersion().c_str()));

  ui->label_qt_version->setText(
    QString("Qt version: ")
    + QString(GetQtVersion().c_str()));

  ui->label_stl_version->setText(
    QString("STL version: ")
    + QString(GetStlVersion().c_str())
    + QString(" (GNU ISO C++ Library)"));

  QObject::connect(ui->button_whats_new,SIGNAL(clicked()),this,SLOT(onWhatsNew()));
  QObject::connect(ui->button_about_qt,SIGNAL(clicked()),this,SLOT(onAboutQt()));
}
//---------------------------------------------------------------------------
DialogAbout::~DialogAbout()
{
  delete ui;
}
//---------------------------------------------------------------------------
void DialogAbout::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------
void DialogAbout::onWhatsNew()
{
  DialogWhatsNew d;
  d.exec();
}
//---------------------------------------------------------------------------
void DialogAbout::onAboutQt()
{
  QApplication::aboutQt();
}
//---------------------------------------------------------------------------
///GetBoostVersion returns the version of the current Boost library.
///From http://www.richelbilderbeek.nl/CppGetBoostVersion.htm
const std::string GetBoostVersion()
{
  std::string s = BOOST_LIB_VERSION;
  std::replace(s.begin(),s.end(),'_','.');
  return s;
}
//---------------------------------------------------------------------------
///GetQtVersion returns the version of the Qt library installed.
///From http://www.richelbilderbeek.nl/CppGetQtVersion.htm
const std::string GetQtVersion()
{
  return QT_VERSION_STR;
}
//---------------------------------------------------------------------------
///GetStlVersion returns the version number of the GCC STL currently installed.
///From http://www.richelbilderbeek.nl/CppGetStlVersion.htm
const std::string GetStlVersion()
{
  return boost::lexical_cast<std::string>(__VERSION__);
}
//---------------------------------------------------------------------------

 

 

 

 

 

dialogabout.h

 

//---------------------------------------------------------------------------
/*
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
//---------------------------------------------------------------------------
#ifndef DIALOGABOUT_H
#define DIALOGABOUT_H
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
namespace Ui {
  class DialogAbout;
}
//---------------------------------------------------------------------------
class DialogAbout : public QDialog
{
  Q_OBJECT

public:
  explicit DialogAbout(QWidget *parent = 0);
  ~DialogAbout();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogAbout *ui;

private slots:
  void onWhatsNew();
  void onAboutQt();
};
//---------------------------------------------------------------------------
#endif // DIALOGABOUT_H
//---------------------------------------------------------------------------
///GetBoostVersion returns the version of the current Boost library.
///From http://www.richelbilderbeek.nl/CppGetBoostVersion.htm
const std::string GetBoostVersion();
//---------------------------------------------------------------------------
///GetQtVersion returns the version of the Qt library installed.
///From http://www.richelbilderbeek.nl/CppGetQtVersion.htm
const std::string GetQtVersion();
//---------------------------------------------------------------------------
///GetStlVersion returns the version number of the GCC STL currently installed.
///From http://www.richelbilderbeek.nl/CppGetStlVersion.htm
const std::string GetStlVersion();
//---------------------------------------------------------------------------

 

 

 

 

 

dialogmain.cpp

 

//---------------------------------------------------------------------------
/*
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;
}
//---------------------------------------------------------------------------

 

 

 

 

 

dialogmain.h

 

//---------------------------------------------------------------------------
/*
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
//---------------------------------------------------------------------------
#ifndef DIALOGMAIN_H
#define DIALOGMAIN_H
//---------------------------------------------------------------------------
#include <QDialog>
#include <boost/regex.hpp>
//---------------------------------------------------------------------------
namespace Ui {
  class DialogMain;
}
//---------------------------------------------------------------------------
class DialogMain : public QDialog
{
  Q_OBJECT

public:
  static const std::string GetVersion() { return "1.2"; }
  explicit DialogMain(QWidget *parent = 0);
  ~DialogMain();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogMain *ui;

private slots:
  void onAnyChange();
  void onAboutClick();
};
//---------------------------------------------------------------------------
const std::vector<std::string> GetRegexMatches(
  const std::string& s,
  const boost::regex& r);
//---------------------------------------------------------------------------
#endif // DIALOGMAIN_H

 

 

 

 

 

dialogwhatsnew.cpp

 

//---------------------------------------------------------------------------
/*
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 "dialogwhatsnew.h"
#include "ui_dialogwhatsnew.h"
//---------------------------------------------------------------------------
DialogWhatsNew::DialogWhatsNew(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogWhatsNew)
{
  ui->setupUi(this);
}
//---------------------------------------------------------------------------
DialogWhatsNew::~DialogWhatsNew()
{
  delete ui;
}
//---------------------------------------------------------------------------
void DialogWhatsNew::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------

 

 

 

 

 

dialogwhatsnew.h

 

//---------------------------------------------------------------------------
/*
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
//---------------------------------------------------------------------------
#ifndef DIALOGWHATSNEW_H
#define DIALOGWHATSNEW_H
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
namespace Ui {
  class DialogWhatsNew;
}
//---------------------------------------------------------------------------
class DialogWhatsNew : public QDialog
{
  Q_OBJECT

public:
  explicit DialogWhatsNew(QWidget *parent = 0);
  ~DialogWhatsNew();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogWhatsNew *ui;
};
//---------------------------------------------------------------------------
#endif // DIALOGWHATSNEW_H

 

 

 

 

 

main.cpp

 

//---------------------------------------------------------------------------
/*
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 <QtGui/QApplication>
#include "dialogmain.h"
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  DialogMain w;
  w.show();
  return a.exec();
}
//---------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict