You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
300 lines
18 KiB
300 lines
18 KiB
<!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 Classes</font></a>
|
|
| <a href="mainclasses.html">
|
|
<font color="#004faf">Main Classes</font></a>
|
|
| <a href="annotated.html">
|
|
<font color="#004faf">Annotated</font></a>
|
|
| <a href="groups.html">
|
|
<font color="#004faf">Grouped 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 <<a href="tqdialog-h.html">tqdialog.h</a>>
|
|
|
|
class TQCheckBox;
|
|
class TQComboBox;
|
|
class TQLabel;
|
|
class TQPushButton;
|
|
class TQStatusBar;
|
|
class TQTable;
|
|
|
|
class RegexpTester : public <a href="tqdialog.html">TQDialog</a>
|
|
{
|
|
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
|
|
|
|
public:
|
|
RegexpTester(TQWidget* parent=0, const char* name=0, bool modal=false,
|
|
WFlags f=0);
|
|
|
|
<a href="tqlabel.html">TQLabel</a> *regexLabel;
|
|
<a href="tqcombobox.html">TQComboBox</a> *regexComboBox;
|
|
<a href="tqlabel.html">TQLabel</a> *textLabel;
|
|
<a href="tqcombobox.html">TQComboBox</a> *textComboBox;
|
|
<a href="tqcheckbox.html">TQCheckBox</a> *caseSensitiveCheckBox;
|
|
<a href="tqcheckbox.html">TQCheckBox</a> *minimalCheckBox;
|
|
<a href="tqcheckbox.html">TQCheckBox</a> *wildcardCheckBox;
|
|
<a href="tqtable.html">TQTable</a> *resultTable;
|
|
<a href="tqpushbutton.html">TQPushButton</a> *executePushButton;
|
|
<a href="tqpushbutton.html">TQPushButton</a> *copyPushButton;
|
|
<a href="tqpushbutton.html">TQPushButton</a> *quitPushButton;
|
|
<a href="tqstatusbar.html">TQStatusBar</a> *statusBar;
|
|
|
|
public slots:
|
|
void copy();
|
|
void execute();
|
|
|
|
private:
|
|
void languageChange();
|
|
};
|
|
|
|
#endif // REGEXPTESTER_H
|
|
</pre>
|
|
|
|
<p> <hr>
|
|
<p> Implementation:
|
|
<p> <pre>#include <<a href="qapplication-h.html">ntqapplication.h</a>>
|
|
#include <<a href="tqcheckbox-h.html">tqcheckbox.h</a>>
|
|
#include <<a href="tqclipboard-h.html">tqclipboard.h</a>>
|
|
#include <<a href="tqcombobox-h.html">tqcombobox.h</a>>
|
|
#include <<a href="tqlabel-h.html">tqlabel.h</a>>
|
|
#include <<a href="tqlayout-h.html">tqlayout.h</a>>
|
|
#include <<a href="tqpushbutton-h.html">tqpushbutton.h</a>>
|
|
#include <<a href="tqregexp-h.html">tqregexp.h</a>>
|
|
#include <<a href="tqstatusbar-h.html">tqstatusbar.h</a>>
|
|
#include <<a href="tqtable-h.html">tqtable.h</a>>
|
|
|
|
#include "regexptester.h"
|
|
|
|
|
|
<a name="f556"></a>RegexpTester::RegexpTester(TQWidget* parent, const char* name, bool modal,
|
|
WFlags f)
|
|
: <a href="tqdialog.html">TQDialog</a>(parent, name, modal, f)
|
|
{
|
|
regexLabel = new <a href="tqlabel.html">TQLabel</a>(this);
|
|
regexComboBox = new <a href="tqcombobox.html">TQComboBox</a>(this);
|
|
<a name="x2480"></a> regexComboBox-><a href="tqcombobox.html#setEditable">setEditable</a>(true);
|
|
<a name="x2507"></a> regexComboBox-><a href="tqwidget.html#setSizePolicy">setSizePolicy</a>(TQSizePolicy::Expanding, TQSizePolicy::Preferred);
|
|
<a name="x2482"></a> regexLabel-><a href="tqlabel.html#setBuddy">setBuddy</a>(regexComboBox);
|
|
textLabel = new <a href="tqlabel.html">TQLabel</a>(this);
|
|
textComboBox = new <a href="tqcombobox.html">TQComboBox</a>(this);
|
|
textComboBox-><a href="tqcombobox.html#setEditable">setEditable</a>(true);
|
|
textComboBox-><a href="tqwidget.html#setSizePolicy">setSizePolicy</a>(TQSizePolicy::Expanding, TQSizePolicy::Preferred);
|
|
textLabel-><a href="tqlabel.html#setBuddy">setBuddy</a>(textComboBox);
|
|
caseSensitiveCheckBox = new <a href="tqcheckbox.html">TQCheckBox</a>(this);
|
|
<a name="x2475"></a> caseSensitiveCheckBox-><a href="tqcheckbox.html#setChecked">setChecked</a>(true);
|
|
minimalCheckBox = new <a href="tqcheckbox.html">TQCheckBox</a>(this);
|
|
wildcardCheckBox = new <a href="tqcheckbox.html">TQCheckBox</a>(this);
|
|
resultTable = new <a href="tqtable.html">TQTable</a>(3, 3, this);
|
|
<a name="x2506"></a> resultTable-><a href="tqtable.html#verticalHeader">verticalHeader</a>()->hide();
|
|
<a name="x2501"></a> resultTable-><a href="tqtable.html#setLeftMargin">setLeftMargin</a>(0);
|
|
<a name="x2499"></a> resultTable-><a href="tqtable.html#horizontalHeader">horizontalHeader</a>()->hide();
|
|
<a name="x2505"></a> resultTable-><a href="tqtable.html#setTopMargin">setTopMargin</a>(0);
|
|
<a name="x2503"></a> resultTable-><a href="tqtable.html#setReadOnly">setReadOnly</a>(true);
|
|
executePushButton = new <a href="tqpushbutton.html">TQPushButton</a>(this);
|
|
<a name="x2484"></a> executePushButton-><a href="tqpushbutton.html#setDefault">setDefault</a>(true);
|
|
copyPushButton = new <a href="tqpushbutton.html">TQPushButton</a>(this);
|
|
quitPushButton = new <a href="tqpushbutton.html">TQPushButton</a>(this);
|
|
statusBar = new <a href="tqstatusbar.html">TQStatusBar</a>(this);
|
|
|
|
<a href="tqgridlayout.html">TQGridLayout</a> *gridLayout = new <a href="tqgridlayout.html">TQGridLayout</a>(2, 2, 6);
|
|
gridLayout-><a href="tqgridlayout.html#addWidget">addWidget</a>(regexLabel, 0, 0);
|
|
gridLayout-><a href="tqgridlayout.html#addWidget">addWidget</a>(regexComboBox, 0, 1);
|
|
gridLayout-><a href="tqgridlayout.html#addWidget">addWidget</a>(textLabel, 1, 0);
|
|
gridLayout-><a href="tqgridlayout.html#addWidget">addWidget</a>(textComboBox, 1, 1);
|
|
<a href="tqhboxlayout.html">TQHBoxLayout</a> *checkboxLayout = new <a href="tqhboxlayout.html">TQHBoxLayout</a>(0, 6, 6);
|
|
checkboxLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(caseSensitiveCheckBox);
|
|
checkboxLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(minimalCheckBox);
|
|
checkboxLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(wildcardCheckBox);
|
|
<a name="x2470"></a> checkboxLayout-><a href="tqboxlayout.html#addStretch">addStretch</a>(1);
|
|
<a href="tqvboxlayout.html">TQVBoxLayout</a> *buttonLayout = new <a href="tqvboxlayout.html">TQVBoxLayout</a>(0, 6, 6);
|
|
buttonLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(executePushButton);
|
|
buttonLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(copyPushButton);
|
|
buttonLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(quitPushButton);
|
|
buttonLayout-><a href="tqboxlayout.html#addStretch">addStretch</a>(1);
|
|
<a href="tqhboxlayout.html">TQHBoxLayout</a> *middleLayout = new <a href="tqhboxlayout.html">TQHBoxLayout</a>(0, 6, 6);
|
|
middleLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(resultTable);
|
|
<a name="x2469"></a> middleLayout-><a href="tqboxlayout.html#addLayout">addLayout</a>(buttonLayout);
|
|
<a href="tqvboxlayout.html">TQVBoxLayout</a> *mainLayout = new <a href="tqvboxlayout.html">TQVBoxLayout</a>(this, 6, 6);
|
|
mainLayout-><a href="tqboxlayout.html#addLayout">addLayout</a>(gridLayout);
|
|
mainLayout-><a href="tqboxlayout.html#addLayout">addLayout</a>(checkboxLayout);
|
|
mainLayout-><a href="tqboxlayout.html#addLayout">addLayout</a>(middleLayout);
|
|
mainLayout-><a href="tqboxlayout.html#addWidget">addWidget</a>(statusBar);
|
|
|
|
<a href="tqwidget.html#resize">resize</a>(TQSize(500, 350).expandedTo(<a href="tqwidget.html#minimumSizeHint">minimumSizeHint</a>()));
|
|
|
|
languageChange();
|
|
|
|
<a href="tqobject.html#connect">connect</a>(copyPushButton, TQ_SIGNAL(<a href="tqbutton.html#clicked">clicked</a>()), this, TQ_SLOT(copy()));
|
|
<a href="tqobject.html#connect">connect</a>(executePushButton, TQ_SIGNAL(<a href="tqbutton.html#clicked">clicked</a>()), this, TQ_SLOT(execute()));
|
|
<a href="tqobject.html#connect">connect</a>(quitPushButton, TQ_SIGNAL(<a href="tqbutton.html#clicked">clicked</a>()), this, TQ_SLOT(<a href="tqdialog.html#accept">accept</a>()));
|
|
|
|
execute();
|
|
}
|
|
|
|
void <a name="f557"></a>RegexpTester::execute()
|
|
{
|
|
<a name="x2478"></a> <a href="tqstring.html">TQString</a> regex = regexComboBox-><a href="tqcombobox.html#currentText">currentText</a>();
|
|
<a href="tqstring.html">TQString</a> text = textComboBox-><a href="tqcombobox.html#currentText">currentText</a>();
|
|
if (!regex.<a href="tqstring.html#isEmpty">isEmpty</a>() && !text.<a href="tqstring.html#isEmpty">isEmpty</a>()) {
|
|
<a href="tqregexp.html">TQRegExp</a> re(regex);
|
|
<a name="x2491"></a><a name="x2474"></a> re.<a href="tqregexp.html#setCaseSensitive">setCaseSensitive</a>(caseSensitiveCheckBox-><a href="tqcheckbox.html#isChecked">isChecked</a>());
|
|
<a name="x2492"></a> re.<a href="tqregexp.html#setMinimal">setMinimal</a>(minimalCheckBox-><a href="tqcheckbox.html#isChecked">isChecked</a>());
|
|
bool wildcard = wildcardCheckBox-><a href="tqcheckbox.html#isChecked">isChecked</a>();
|
|
<a name="x2493"></a> re.<a href="tqregexp.html#setWildcard">setWildcard</a>(wildcard);
|
|
<a name="x2487"></a> if (!re.<a href="tqregexp.html#isValid">isValid</a>()) {
|
|
<a name="x2494"></a> statusBar-><a href="tqstatusbar.html#message">message</a>(<a href="tqobject.html#tr">tr</a>("Invalid <a href="tqregexp.html#regular-expression">regular expression</a>: %1")
|
|
<a name="x2486"></a> .arg(re.<a href="tqregexp.html#errorString">errorString</a>()));
|
|
return;
|
|
}
|
|
<a name="x2490"></a> int offset = re.<a href="tqregexp.html#search">search</a>(text);
|
|
<a name="x2489"></a> int captures = re.<a href="tqregexp.html#numCaptures">numCaptures</a>();
|
|
int row = 0;
|
|
const int OFFSET = 5;
|
|
<a name="x2502"></a> resultTable-><a href="tqtable.html#setNumRows">setNumRows</a>(0);
|
|
resultTable-><a href="tqtable.html#setNumRows">setNumRows</a>(captures + OFFSET);
|
|
<a name="x2504"></a> resultTable-><a href="tqtable.html#setText">setText</a>(row, 0, tr("Regex"));
|
|
<a href="tqstring.html">TQString</a> escaped = regex;
|
|
<a name="x2497"></a> escaped = escaped.<a href="tqstring.html#replace">replace</a>("\\", "\\\\");
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 1, escaped);
|
|
<a name="x2500"></a> resultTable-><a href="tqtable.html#item">item</a>(row, 1)->setSpan(1, 2);
|
|
if (offset != -1) {
|
|
++row;
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 0, tr("Offset"));
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 1, TQString::number(offset));
|
|
resultTable-><a href="tqtable.html#item">item</a>(row, 1)->setSpan(1, 2);
|
|
if (!wildcard) {
|
|
++row;
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 0, tr("Captures"));
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 1, TQString::number(captures));
|
|
resultTable-><a href="tqtable.html#item">item</a>(row, 1)->setSpan(1, 2);
|
|
++row;
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 1, tr("Text"));
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 2, tr("Characters"));
|
|
}
|
|
++row;
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 0, tr("Match"));
|
|
<a name="x2485"></a> resultTable-><a href="tqtable.html#setText">setText</a>(row, 1, re.<a href="tqregexp.html#cap">cap</a>(0));
|
|
<a name="x2488"></a> resultTable-><a href="tqtable.html#setText">setText</a>(row, 2, TQString::number(re.<a href="tqregexp.html#matchedLength">matchedLength</a>()));
|
|
if (!wildcard) {
|
|
for (int i = 1; i <= captures; ++i) {
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row + i, 0, tr("Capture #%1").arg(i));
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row + i, 1, re.<a href="tqregexp.html#cap">cap</a>(i));
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row + i, 2,
|
|
<a name="x2496"></a> TQString::<a href="tqstring.html#number">number</a>(re.<a href="tqregexp.html#cap">cap</a>(i).length()));
|
|
}
|
|
}
|
|
else
|
|
resultTable-><a href="tqtable.html#setNumRows">setNumRows</a>(3);
|
|
}
|
|
else {
|
|
resultTable-><a href="tqtable.html#setNumRows">setNumRows</a>(2);
|
|
++row;
|
|
resultTable-><a href="tqtable.html#setText">setText</a>(row, 0, tr("No matches"));
|
|
resultTable-><a href="tqtable.html#item">item</a>(row, 0)->setSpan(1, 3);
|
|
}
|
|
<a name="x2498"></a> resultTable-><a href="tqtable.html#adjustColumn">adjustColumn</a>(0);
|
|
resultTable-><a href="tqtable.html#adjustColumn">adjustColumn</a>(1);
|
|
resultTable-><a href="tqtable.html#adjustColumn">adjustColumn</a>(2);
|
|
statusBar-><a href="tqstatusbar.html#message">message</a>(<a href="tqobject.html#tr">tr</a>("Executed \"%1\" on \"%2\"")
|
|
.arg(escaped).arg(text));
|
|
}
|
|
else
|
|
statusBar-><a href="tqstatusbar.html#message">message</a>(<a href="tqobject.html#tr">tr</a>("A regular expression and a text must be given"));
|
|
}
|
|
|
|
void <a name="f558"></a>RegexpTester::copy()
|
|
{
|
|
<a href="tqstring.html">TQString</a> escaped = regexComboBox-><a href="tqcombobox.html#currentText">currentText</a>();
|
|
if (!escaped.<a href="tqstring.html#isEmpty">isEmpty</a>()) {
|
|
escaped = escaped.<a href="tqstring.html#replace">replace</a>("\\", "\\\\");
|
|
<a name="x2468"></a> <a href="tqclipboard.html">TQClipboard</a> *cb = TQApplication::<a href="ntqapplication.html#clipboard">clipboard</a>();
|
|
<a name="x2476"></a> cb-><a href="tqclipboard.html#setText">setText</a>(escaped, TQClipboard::Clipboard);
|
|
<a name="x2477"></a> if (cb-><a href="tqclipboard.html#supportsSelection">supportsSelection</a>())
|
|
cb-><a href="tqclipboard.html#setText">setText</a>(escaped, TQClipboard::Selection);
|
|
statusBar-><a href="tqstatusbar.html#message">message</a>(<a href="tqobject.html#tr">tr</a>("Copied \"%1\" to the clipboard")
|
|
.arg(escaped));
|
|
}
|
|
}
|
|
|
|
void <a name="f559"></a>RegexpTester::languageChange()
|
|
{
|
|
<a href="tqwidget.html#setCaption">setCaption</a>(<a href="tqobject.html#tr">tr</a>("Regex Tester"));
|
|
regexLabel-><a href="tqlabel.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Regex:"));
|
|
<a name="x2479"></a> regexComboBox-><a href="tqcombobox.html#insertItem">insertItem</a>(<a href="tqobject.html#tr">tr</a>("[A-Z]+=(\\d+):(\\d*)"));
|
|
textLabel-><a href="tqlabel.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Text:"));
|
|
textComboBox-><a href="tqcombobox.html#insertItem">insertItem</a>(<a href="tqobject.html#tr">tr</a>("ABC=12:3456"));
|
|
<a name="x2473"></a> caseSensitiveCheckBox-><a href="tqbutton.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("Case &Sensitive"));
|
|
minimalCheckBox-><a href="tqbutton.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Minimal"));
|
|
wildcardCheckBox-><a href="tqbutton.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Wildcard"));
|
|
copyPushButton-><a href="tqbutton.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Copy"));
|
|
executePushButton-><a href="tqbutton.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Execute"));
|
|
quitPushButton-><a href="tqbutton.html#setText">setText</a>(<a href="tqobject.html#tr">tr</a>("&Quit"));
|
|
}
|
|
|
|
</pre>
|
|
|
|
<p> <hr>
|
|
<p> Main:
|
|
<p> <pre>#include <<a href="qapplication-h.html">ntqapplication.h</a>>
|
|
#include "regexptester.h"
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
<a href="ntqapplication.html">TQApplication</a> app(argc, argv);
|
|
RegexpTester form;
|
|
<a name="x2511"></a> form.<a href="tqdialog.html#show">show</a>();
|
|
<a name="x2512"></a><a name="x2510"></a><a name="x2509"></a> app.<a href="tqobject.html#connect">connect</a>(&app, TQ_SIGNAL(<a href="ntqapplication.html#lastWindowClosed">lastWindowClosed</a>()), &app, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()));
|
|
return app.<a href="ntqapplication.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 © 2007
|
|
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
|
<td align=right><div align=right>TQt 3.3.8</div>
|
|
</table></div></address></body>
|
|
</html>
|