Qt 正则表达式简单测试
来源:互联网 发布:二战网络射击游戏 编辑:程序博客网 时间:2024/06/13 06:08
对正则表达式以前没了解过,这次用到大概看了一下,感觉挺复杂的,没怎么太懂,所以对 QRegExp 也没完全理解,就直接贴代码了:
main.cpp
以下是测试结果:
regexp.h
- #ifndef REGEXP_H
- #define REGEXP_H
- #include <QtGui/QDialog>
- #include <QtGui/QLineEdit>
- #include <QtGui/QTextEdit>
- #include <QtGui/QLabel>
- #include <QtGui/QPushButton>
- #include <QtGui/QListWidget>
- #include <QtGui/QVBoxLayout>
- class RegexpConner : public QDialog
- {
- Q_OBJECT
- public:
- RegexpConner( QWidget *parent = 0, Qt::WFlags flags = 0 );
- ~RegexpConner();
- private slots:
- void onGet ();
- private:
- QLabel *m_labelRegexp;
- QLabel *m_labelText;
- QLabel *m_labelResult;
- QLabel *m_labelIndex;
- QLineEdit *m_leditRegexp;
- QLineEdit *m_leditIndex;
- QTextEdit *m_teditText;
- QListWidget *m_listResult;
- QPushButton *m_btnGet;
- QHBoxLayout *m_hboxLayout;
- QVBoxLayout *m_vboxLayout;
- };
- #endif // REGEXP_H
- #include "regexp.h"
- RegexpConner::RegexpConner( QWidget *parent, Qt::WFlags flags )
- : QDialog( parent, flags )
- {
- m_labelRegexp = new QLabel( "regexp:" );
- m_labelText = new QLabel( "text:" );
- m_labelResult = new QLabel( "result:" );
- m_labelIndex = new QLabel( "index:" );
- m_leditRegexp = new QLineEdit;
- m_leditIndex = new QLineEdit;
- m_teditText = new QTextEdit;
- m_listResult = new QListWidget;
- m_btnGet = new QPushButton( "GET" );
- m_hboxLayout = new QHBoxLayout;
- m_hboxLayout->addWidget( m_btnGet );
- m_hboxLayout->addWidget( m_labelIndex );
- m_hboxLayout->addWidget( m_leditIndex );
- m_vboxLayout = new QVBoxLayout( this );
- m_vboxLayout->addWidget( m_labelText );
- m_vboxLayout->addWidget( m_teditText );
- m_vboxLayout->addWidget( m_labelRegexp );
- m_vboxLayout->addWidget( m_leditRegexp );
- m_vboxLayout->addLayout( m_hboxLayout );
- m_vboxLayout->addWidget( m_labelResult );
- m_vboxLayout->addWidget( m_listResult );
- setFixedSize( 500, 500 );
- connect( m_btnGet, SIGNAL(clicked()), this, SLOT(onGet()) );
- }
- RegexpConner::~RegexpConner()
- {
- }
- void RegexpConner::onGet()
- {
- m_listResult->clear();
- QString text = m_teditText->toPlainText();
- QString exp = m_leditRegexp->text();
- if( text.isEmpty() || exp.isEmpty() )
- return;
- int index = m_leditIndex->text().toUInt();
- QRegExp qregexp( exp );
- int length = 0;
- int idx = qregexp.indexIn( text, 0 );
- while( -1 != idx ) {
- int num = qregexp.numCaptures();
- QString str = qregexp.cap( index );
- if( !str.isEmpty() ) {
- QListWidgetItem *item = new QListWidgetItem( str );
- m_listResult->addItem( item );
- }
- length = qregexp.matchedLength();
- idx = qregexp.indexIn( text, idx + length );
- }
- }
main.cpp
- #include "regexp.h"
- #include <QtGui/QApplication>
- #include <QRegExp>
- #include <QTextCodec>
- #include <QObject>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- RegexpConner rc;
- rc.show();
- return a.exec();
- }
以下是测试结果:
转载:http://blog.csdn.net/kusey/article/details/7383184
- Qt 正则表达式简单测试
- Qt 正则表达式简单测
- Qt实现实时正则表达式测试器
- QT中正则表达式的简单说明
- Qt正则表达式
- QT的正则表达式
- Qt中的正则表达式
- QT之正则表达式
- QT的正则表达式
- QT中的正则表达式
- QT的正则表达式
- Qt正则表达式
- qt 正则表达式
- Qt 正则表达式
- Qt之正则表达式
- qt之正则表达式
- Qt 正则表达式 例子
- Qt正则表达式
- getHibernateTemplate与getSession
- C++ GUI Qt4学习笔记(九)
- 链表
- 敏捷开发松结对编程系列之十:代码审查最佳实践
- 随笔八:存储器层次结构
- Qt 正则表达式简单测试
- 1099
- HTML特殊字符表
- struts2.0的Demo
- MySQL中UNION和UNION ALL的使用
- speeding up app in android
- MS Chart Control - X轴 项目超9项时 Label显示不完全(已解决)
- Android---NDK编译静态库失败
- struts 2-002-数据校验的应用