Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Using Regex In Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Platform(s): All Qt Supported
Keywords: Qt Application
Created: skumar_rao (26 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)


This article shows how to use regular expression with Qt. "Regular expressions" or Regex for short are strings that describe a pattern, they are very powerful tool for lexical analyzer, but it only one use of the Regex it is used in other purposes are also.

We will first look at the syntax of Regex then we will see how to use them with Qt .

  • Most characters have no special meaning and can be used as is, except a few who will be escaped:

Accept all strings possible with the letter r.


Accept all strings containing "ro", like "roi", "rose", "arthrose" but not "rio".

To leave the choice between several characters, it must be put in square brackets, thus


Accept "frotte" or "trotte", but not "carotte".

To specify a lot of characters (in sequence), you can specify the start and end separated by a hyphen, eg:


you can find more info in

QRegExp has three constructors

  • A constructor without arguments builds a Regex empty.
  • A constructor that takes a QString argument representing the Regex.
  • A constructor that takes three arguments: Regex, a parameter to determine whether the Regex is case sensitive and the type used Regex (QRegExp:: Regex RegExp for PERL, QRegExp: regexp2.
 QRegExp mailExp ( " ^[\w|\.]+@[\w]+\.[\w]{2,4}$ " );


#ifndef REGEXQT_H
#define REGEXQT_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class regexqt : public QDialog
public slots:
void OnConfirm(QRegExp mailExp);
QLabel* label;
QLineEdit* mailEntry;
QPushButton* confirmButton;
QRegExp mailExp;


#include <QtGui>
#include "regexqt.h"
regexqt::regexqt ()
/* définition du titre de la fenêtre - definition of the window title */
setWindowTitle ("Validation");
/* création des contrôles - establishment of controls*/
label = new QLabel ("Entrez votre chaine de caracteres Enter your string:");
Entry = new QLineEdit;
confirmButton = new QPushButton ("&Confirmer");
label->setBuddy (Entry);
/* conexion du signal clicked pour le bouton confirmer conexion signal clicked the button to confirm */
/* conexion signal clicked the button to confirm signal clicked conexion The Button to confirm */
connect (confirmButton, SIGNAL (clicked ()), this, SLOT (OnConfirm ()));
/* création du layout - creation of the layout*/
QHBoxLayout* centralLayout = new QHBoxLayout;
centralLayout->addWidget (label);
centralLayout->addWidget (Entry);
centralLayout->addWidget (confirmButton);
setLayout (centralLayout);
/* création de la Regex - creating the Regex*/
QRegExp Exp ("^[\\w|\\.]+@[\\w]+\\.[\\w]{2,4}$");
mailExp = Exp ;
void regexqt::OnConfirm ()
QString EntryText = Entry->text ();
if (EntryText.contains (mailExp))
QMessageBox::information (this, "Validation", "Valide !");
QMessageBox::critical (this, "Validation", EntryText + " n'est pas valide - is not valid!");
regexqt::~regexqt ()

Related Article


--skumar_rao 15:48, 26 November 2010 (UTC)

This page was last modified on 11 October 2012, at 01:18.
64 page views in the last 30 days.