×
Namespaces

Variants
Actions

Using Regex In Qt

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


Introduction

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:
r

Accept all strings possible with the letter r.

ro

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

[ft]rotte

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:

[az]

you can find more info in http://doc.trolltech.com/latest/qregexp.html


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}$ " );

regexqt.h

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

regexqt.cpp

#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 !");
else
QMessageBox::critical (this, "Validation", EntryText + " n'est pas valide - is not valid!");
return;
}
 
regexqt::~regexqt ()
{
}

Related Article

QRegExpValidator


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

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

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×