summaryrefslogtreecommitdiffstats
path: root/doc/html/regexptester-example.html
blob: 982ce82894cfde5b47c5b52dab5713c38309ddda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/regexptester/regexptester.doc:1 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A Small Application for Testing Regular Expressions</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A Small Application for Testing Regular Expressions</h1>



<p> 
<p> Regular expressions can sometimes be tricky to get right,
especially those that use the * quantifier. This application lets
you type in a regexp (without doubling the backslashes) and some
test text, and to execute the regexp and see the results. If you
click the Copy button the regexp will be copied to the clipboard
(with the backslashes doubled, ready for you to paste into your
program). Previous regexps and test texts are remembered
throughout the session and can be accessed by dropping down the
comboboxes.
<p> <hr>
<p> Header file:
<p> <pre>#ifndef REGEXPTESTER_H
#define REGEXPTESTER_H

#include &lt;<a href="qdialog-h.html">qdialog.h</a>&gt;

class QCheckBox;
class QComboBox;
class QLabel;
class QPushButton;
class QStatusBar;
class QTable;

class RegexpTester : public <a href="qdialog.html">QDialog</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

public:
    RegexpTester(QWidget* parent=0, const char* name=0, bool modal=false,
                 WFlags f=0);

    <a href="qlabel.html">QLabel</a> *regexLabel;
    <a href="qcombobox.html">QComboBox</a> *regexComboBox;
    <a href="qlabel.html">QLabel</a> *textLabel;
    <a href="qcombobox.html">QComboBox</a> *textComboBox;
    <a href="qcheckbox.html">QCheckBox</a> *caseSensitiveCheckBox;
    <a href="qcheckbox.html">QCheckBox</a> *minimalCheckBox;
    <a href="qcheckbox.html">QCheckBox</a> *wildcardCheckBox;
    <a href="qtable.html">QTable</a> *resultTable;
    <a href="qpushbutton.html">QPushButton</a> *executePushButton;
    <a href="qpushbutton.html">QPushButton</a> *copyPushButton;
    <a href="qpushbutton.html">QPushButton</a> *quitPushButton;
    <a href="qstatusbar.html">QStatusBar</a> *statusBar;

public slots:
    void copy();
    void execute();

private:
    void languageChange();
};

#endif // REGEXPTESTER_H
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;
#include &lt;<a href="qclipboard-h.html">qclipboard.h</a>&gt;
#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qregexp-h.html">qregexp.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qtable-h.html">qtable.h</a>&gt;

#include "regexptester.h"


<a name="f556"></a>RegexpTester::RegexpTester(QWidget* parent, const char* name, bool modal,
                           WFlags f)
    : <a href="qdialog.html">QDialog</a>(parent, name, modal, f)
{
    regexLabel = new <a href="qlabel.html">QLabel</a>(this);
    regexComboBox = new <a href="qcombobox.html">QComboBox</a>(this);
<a name="x2480"></a>    regexComboBox-&gt;<a href="qcombobox.html#setEditable">setEditable</a>(true);
<a name="x2507"></a>    regexComboBox-&gt;<a href="qwidget.html#setSizePolicy">setSizePolicy</a>(QSizePolicy::Expanding, QSizePolicy::Preferred);
<a name="x2482"></a>    regexLabel-&gt;<a href="qlabel.html#setBuddy">setBuddy</a>(regexComboBox);
    textLabel = new <a href="qlabel.html">QLabel</a>(this);
    textComboBox = new <a href="qcombobox.html">QComboBox</a>(this);
    textComboBox-&gt;<a href="qcombobox.html#setEditable">setEditable</a>(true);
    textComboBox-&gt;<a href="qwidget.html#setSizePolicy">setSizePolicy</a>(QSizePolicy::Expanding, QSizePolicy::Preferred);
    textLabel-&gt;<a href="qlabel.html#setBuddy">setBuddy</a>(textComboBox);
    caseSensitiveCheckBox = new <a href="qcheckbox.html">QCheckBox</a>(this);
<a name="x2475"></a>    caseSensitiveCheckBox-&gt;<a href="qcheckbox.html#setChecked">setChecked</a>(true);
    minimalCheckBox = new <a href="qcheckbox.html">QCheckBox</a>(this);
    wildcardCheckBox = new <a href="qcheckbox.html">QCheckBox</a>(this);
    resultTable = new <a href="qtable.html">QTable</a>(3, 3, this);
<a name="x2506"></a>    resultTable-&gt;<a href="qtable.html#verticalHeader">verticalHeader</a>()-&gt;hide();
<a name="x2501"></a>    resultTable-&gt;<a href="qtable.html#setLeftMargin">setLeftMargin</a>(0);
<a name="x2499"></a>    resultTable-&gt;<a href="qtable.html#horizontalHeader">horizontalHeader</a>()-&gt;hide();
<a name="x2505"></a>    resultTable-&gt;<a href="qtable.html#setTopMargin">setTopMargin</a>(0);
<a name="x2503"></a>    resultTable-&gt;<a href="qtable.html#setReadOnly">setReadOnly</a>(true);
    executePushButton = new <a href="qpushbutton.html">QPushButton</a>(this);
<a name="x2484"></a>    executePushButton-&gt;<a href="qpushbutton.html#setDefault">setDefault</a>(true);
    copyPushButton = new <a href="qpushbutton.html">QPushButton</a>(this);
    quitPushButton = new <a href="qpushbutton.html">QPushButton</a>(this);
    statusBar = new <a href="qstatusbar.html">QStatusBar</a>(this);

    <a href="qgridlayout.html">QGridLayout</a> *gridLayout = new <a href="qgridlayout.html">QGridLayout</a>(2, 2, 6);
    gridLayout-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>(regexLabel, 0, 0);
    gridLayout-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>(regexComboBox, 0, 1);
    gridLayout-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>(textLabel, 1, 0);
    gridLayout-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>(textComboBox, 1, 1);
    <a href="qhboxlayout.html">QHBoxLayout</a> *checkboxLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>(0, 6, 6);
    checkboxLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(caseSensitiveCheckBox);
    checkboxLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(minimalCheckBox);
    checkboxLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(wildcardCheckBox);
<a name="x2470"></a>    checkboxLayout-&gt;<a href="qboxlayout.html#addStretch">addStretch</a>(1);
    <a href="qvboxlayout.html">QVBoxLayout</a> *buttonLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>(0, 6, 6);
    buttonLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(executePushButton);
    buttonLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(copyPushButton);
    buttonLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(quitPushButton);
    buttonLayout-&gt;<a href="qboxlayout.html#addStretch">addStretch</a>(1);
    <a href="qhboxlayout.html">QHBoxLayout</a> *middleLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>(0, 6, 6);
    middleLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(resultTable);
<a name="x2469"></a>    middleLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>(buttonLayout);
    <a href="qvboxlayout.html">QVBoxLayout</a> *mainLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>(this, 6, 6);
    mainLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>(gridLayout);
    mainLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>(checkboxLayout);
    mainLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>(middleLayout);
    mainLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>(statusBar);

    <a href="qwidget.html#resize">resize</a>(QSize(500, 350).expandedTo(<a href="qwidget.html#minimumSizeHint">minimumSizeHint</a>()));

    languageChange();

    <a href="qobject.html#connect">connect</a>(copyPushButton, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(copy()));
    <a href="qobject.html#connect">connect</a>(executePushButton, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(execute()));
    <a href="qobject.html#connect">connect</a>(quitPushButton, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(<a href="qdialog.html#accept">accept</a>()));

    execute();
}

void <a name="f557"></a>RegexpTester::execute()
{
<a name="x2478"></a>    <a href="qstring.html">QString</a> regex = regexComboBox-&gt;<a href="qcombobox.html#currentText">currentText</a>();
    <a href="qstring.html">QString</a> text = textComboBox-&gt;<a href="qcombobox.html#currentText">currentText</a>();
    if (!regex.<a href="qstring.html#isEmpty">isEmpty</a>() &amp;&amp; !text.<a href="qstring.html#isEmpty">isEmpty</a>()) {
        <a href="qregexp.html">QRegExp</a> re(regex);
<a name="x2491"></a><a name="x2474"></a>        re.<a href="qregexp.html#setCaseSensitive">setCaseSensitive</a>(caseSensitiveCheckBox-&gt;<a href="qcheckbox.html#isChecked">isChecked</a>());
<a name="x2492"></a>        re.<a href="qregexp.html#setMinimal">setMinimal</a>(minimalCheckBox-&gt;<a href="qcheckbox.html#isChecked">isChecked</a>());
        bool wildcard = wildcardCheckBox-&gt;<a href="qcheckbox.html#isChecked">isChecked</a>();
<a name="x2493"></a>        re.<a href="qregexp.html#setWildcard">setWildcard</a>(wildcard);
<a name="x2487"></a>        if (!re.<a href="qregexp.html#isValid">isValid</a>()) {
<a name="x2494"></a>            statusBar-&gt;<a href="qstatusbar.html#message">message</a>(<a href="qobject.html#tr">tr</a>("Invalid <a href="qregexp.html#regular-expression">regular expression</a>: %1")
<a name="x2486"></a>                                .arg(re.<a href="qregexp.html#errorString">errorString</a>()));
            return;
        }
<a name="x2490"></a>        int offset = re.<a href="qregexp.html#search">search</a>(text);
<a name="x2489"></a>        int captures = re.<a href="qregexp.html#numCaptures">numCaptures</a>();
        int row = 0;
        const int OFFSET = 5;
<a name="x2502"></a>        resultTable-&gt;<a href="qtable.html#setNumRows">setNumRows</a>(0);
        resultTable-&gt;<a href="qtable.html#setNumRows">setNumRows</a>(captures + OFFSET);
<a name="x2504"></a>        resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 0, tr("Regex"));
        <a href="qstring.html">QString</a> escaped = regex;
<a name="x2497"></a>        escaped = escaped.<a href="qstring.html#replace">replace</a>("\\", "\\\\");
        resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 1, escaped);
<a name="x2500"></a>        resultTable-&gt;<a href="qtable.html#item">item</a>(row, 1)-&gt;setSpan(1, 2);
        if (offset != -1) {
            ++row;
            resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 0, tr("Offset"));
            resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 1, QString::number(offset));
            resultTable-&gt;<a href="qtable.html#item">item</a>(row, 1)-&gt;setSpan(1, 2);
            if (!wildcard) {
                ++row;
                resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 0, tr("Captures"));
                resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 1, QString::number(captures));
                resultTable-&gt;<a href="qtable.html#item">item</a>(row, 1)-&gt;setSpan(1, 2);
                ++row;
                resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 1, tr("Text"));
                resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 2, tr("Characters"));
            }
            ++row;
            resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 0, tr("Match"));
<a name="x2485"></a>            resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 1, re.<a href="qregexp.html#cap">cap</a>(0));
<a name="x2488"></a>            resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 2, QString::number(re.<a href="qregexp.html#matchedLength">matchedLength</a>()));
            if (!wildcard) {
                for (int i = 1; i &lt;= captures; ++i) {
                    resultTable-&gt;<a href="qtable.html#setText">setText</a>(row + i, 0, tr("Capture #%1").arg(i));
                    resultTable-&gt;<a href="qtable.html#setText">setText</a>(row + i, 1, re.<a href="qregexp.html#cap">cap</a>(i));
                    resultTable-&gt;<a href="qtable.html#setText">setText</a>(row + i, 2,
<a name="x2496"></a>                                        QString::<a href="qstring.html#number">number</a>(re.<a href="qregexp.html#cap">cap</a>(i).length()));
                }
            }
            else
                resultTable-&gt;<a href="qtable.html#setNumRows">setNumRows</a>(3);
        }
        else {
            resultTable-&gt;<a href="qtable.html#setNumRows">setNumRows</a>(2);
            ++row;
            resultTable-&gt;<a href="qtable.html#setText">setText</a>(row, 0, tr("No matches"));
            resultTable-&gt;<a href="qtable.html#item">item</a>(row, 0)-&gt;setSpan(1, 3);
        }
<a name="x2498"></a>        resultTable-&gt;<a href="qtable.html#adjustColumn">adjustColumn</a>(0);
        resultTable-&gt;<a href="qtable.html#adjustColumn">adjustColumn</a>(1);
        resultTable-&gt;<a href="qtable.html#adjustColumn">adjustColumn</a>(2);
        statusBar-&gt;<a href="qstatusbar.html#message">message</a>(<a href="qobject.html#tr">tr</a>("Executed \"%1\" on \"%2\"")
                                .arg(escaped).arg(text));
    }
    else
        statusBar-&gt;<a href="qstatusbar.html#message">message</a>(<a href="qobject.html#tr">tr</a>("A regular expression and a text must be given"));
}

void <a name="f558"></a>RegexpTester::copy()
{
    <a href="qstring.html">QString</a> escaped = regexComboBox-&gt;<a href="qcombobox.html#currentText">currentText</a>();
    if (!escaped.<a href="qstring.html#isEmpty">isEmpty</a>()) {
        escaped = escaped.<a href="qstring.html#replace">replace</a>("\\", "\\\\");
<a name="x2468"></a>        <a href="qclipboard.html">QClipboard</a> *cb = QApplication::<a href="qapplication.html#clipboard">clipboard</a>();
<a name="x2476"></a>        cb-&gt;<a href="qclipboard.html#setText">setText</a>(escaped, QClipboard::Clipboard);
<a name="x2477"></a>        if (cb-&gt;<a href="qclipboard.html#supportsSelection">supportsSelection</a>())
            cb-&gt;<a href="qclipboard.html#setText">setText</a>(escaped, QClipboard::Selection);
        statusBar-&gt;<a href="qstatusbar.html#message">message</a>(<a href="qobject.html#tr">tr</a>("Copied \"%1\" to the clipboard")
                                .arg(escaped));
    }
}

void <a name="f559"></a>RegexpTester::languageChange()
{
    <a href="qwidget.html#setCaption">setCaption</a>(<a href="qobject.html#tr">tr</a>("Regex Tester"));
    regexLabel-&gt;<a href="qlabel.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Regex:"));
<a name="x2479"></a>    regexComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>(<a href="qobject.html#tr">tr</a>("[A-Z]+=(\\d+):(\\d*)"));
    textLabel-&gt;<a href="qlabel.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Text:"));
    textComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>(<a href="qobject.html#tr">tr</a>("ABC=12:3456"));
<a name="x2473"></a>    caseSensitiveCheckBox-&gt;<a href="qbutton.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("Case &amp;Sensitive"));
    minimalCheckBox-&gt;<a href="qbutton.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Minimal"));
    wildcardCheckBox-&gt;<a href="qbutton.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Wildcard"));
    copyPushButton-&gt;<a href="qbutton.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Copy"));
    executePushButton-&gt;<a href="qbutton.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Execute"));
    quitPushButton-&gt;<a href="qbutton.html#setText">setText</a>(<a href="qobject.html#tr">tr</a>("&amp;Quit"));
}

</pre>

<p> <hr>
<p> Main:
<p> <pre>#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include "regexptester.h"

int main(int argc, char **argv)
{
    <a href="qapplication.html">QApplication</a> app(argc, argv);
    RegexpTester form;
<a name="x2511"></a>    form.<a href="qdialog.html#show">show</a>();
<a name="x2512"></a><a name="x2510"></a><a name="x2509"></a>    app.<a href="qobject.html#connect">connect</a>(&amp;app, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &amp;app, SLOT(<a href="qapplication.html#quit">quit</a>()));
    return app.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>