Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's tools.

 

 

 

 

 

(Tool) RegexTester source code (version 1.0)

 

RegexTester source code (version 1.0).

 

 

 

 

 

Operating system: Ubuntu 10.04 Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: Console Application

Compiler: G++ 4.4.1

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
HEADERS += dialogmain.h \
dialogabout.h
FORMS += dialogmain.ui \
dialogabout.ui
RESOURCES += \
resources.qrc

 

 

 

 

 

Source code

 

 

 

 

 

dialogabout.h

 

#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;
};

#endif // DIALOGABOUT_H

 

 

 

 

 

dialogabout.cpp

 

#include "dialogabout.h"
#include "ui_dialogabout.h"

DialogAbout::DialogAbout(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogAbout)
{
  ui->setupUi(this);
}

DialogAbout::~DialogAbout()
{
  delete ui;
}

void DialogAbout::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}

 

 

 

 

 

dialogmain.h

 

#ifndef DIALOGMAIN_H
#define DIALOGMAIN_H

#include <QDialog>

namespace Ui {
  class DialogMain;
}

class DialogMain : public QDialog
{
  Q_OBJECT

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

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogMain *ui;

private slots:
  void onAnyChange();
  void onAboutClick();
};

#endif // DIALOGMAIN_H

 

 

 

 

 

dialogmain.cpp

 

#include <cassert>
#include <string>
#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("'1234 AB' is a valid Dutch zip code");
  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->label_regex_found->setText("Regex found in line: "
    + QString(boost::regex_search(s,r) ? "yes" : "no"));
}

void DialogMain::onAboutClick()
{
  DialogAbout d;
  d.exec();
}

 

 

 

 

 

main.cpp

 

#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 tools.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict