Qt 正则表达式简单测试

来源:互联网 发布:二战网络射击游戏 编辑:程序博客网 时间:2024/06/13 06:08
对正则表达式以前没了解过,这次用到大概看了一下,感觉挺复杂的,没怎么太懂,所以对 QRegExp 也没完全理解,就直接贴代码了:


regexp.h

[cpp] view plaincopy
  1. #ifndef REGEXP_H  
  2. #define REGEXP_H  
  3.   
  4. #include <QtGui/QDialog>  
  5. #include <QtGui/QLineEdit>  
  6. #include <QtGui/QTextEdit>  
  7. #include <QtGui/QLabel>  
  8. #include <QtGui/QPushButton>  
  9. #include <QtGui/QListWidget>  
  10. #include <QtGui/QVBoxLayout>  
  11.   
  12. class RegexpConner : public QDialog  
  13. {  
  14.     Q_OBJECT  
  15.   
  16. public:  
  17.     RegexpConner( QWidget *parent = 0, Qt::WFlags flags = 0 );  
  18.     ~RegexpConner();  
  19.   
  20. private slots:  
  21.     void    onGet   ();  
  22.   
  23. private:  
  24.     QLabel      *m_labelRegexp;  
  25.     QLabel      *m_labelText;  
  26.     QLabel      *m_labelResult;  
  27.     QLabel      *m_labelIndex;  
  28.     QLineEdit   *m_leditRegexp;  
  29.     QLineEdit   *m_leditIndex;  
  30.     QTextEdit   *m_teditText;  
  31.     QListWidget *m_listResult;  
  32.     QPushButton *m_btnGet;  
  33.     QHBoxLayout *m_hboxLayout;  
  34.     QVBoxLayout *m_vboxLayout;  
  35. };  
  36.   
  37. #endif // REGEXP_H  


regexp.cpp
[cpp] view plaincopy
  1. #include "regexp.h"  
  2.   
  3. RegexpConner::RegexpConner( QWidget *parent, Qt::WFlags flags )  
  4.     : QDialog( parent, flags )  
  5. {  
  6.     m_labelRegexp   = new QLabel( "regexp:" );  
  7.     m_labelText     = new QLabel( "text:" );  
  8.     m_labelResult   = new QLabel( "result:" );  
  9.     m_labelIndex    = new QLabel( "index:" );  
  10.     m_leditRegexp   = new QLineEdit;  
  11.     m_leditIndex    = new QLineEdit;  
  12.     m_teditText     = new QTextEdit;  
  13.     m_listResult    = new QListWidget;  
  14.     m_btnGet        = new QPushButton( "GET" );  
  15.   
  16.     m_hboxLayout    = new QHBoxLayout;  
  17.     m_hboxLayout->addWidget( m_btnGet );  
  18.     m_hboxLayout->addWidget( m_labelIndex );  
  19.     m_hboxLayout->addWidget( m_leditIndex );  
  20.   
  21.     m_vboxLayout    = new QVBoxLayout( this );  
  22.     m_vboxLayout->addWidget( m_labelText );  
  23.     m_vboxLayout->addWidget( m_teditText );  
  24.     m_vboxLayout->addWidget( m_labelRegexp );  
  25.     m_vboxLayout->addWidget( m_leditRegexp );  
  26.     m_vboxLayout->addLayout( m_hboxLayout );  
  27.     m_vboxLayout->addWidget( m_labelResult );  
  28.     m_vboxLayout->addWidget( m_listResult );  
  29.   
  30.     setFixedSize( 500, 500 );  
  31.   
  32.     connect( m_btnGet, SIGNAL(clicked()), this, SLOT(onGet()) );  
  33. }  
  34.   
  35. RegexpConner::~RegexpConner()  
  36. {  
  37. }  
  38.   
  39. void RegexpConner::onGet()  
  40. {  
  41.     m_listResult->clear();  
  42.   
  43.     QString text = m_teditText->toPlainText();  
  44.     QString exp = m_leditRegexp->text();  
  45.     if( text.isEmpty() || exp.isEmpty() )  
  46.         return;  
  47.   
  48.     int index = m_leditIndex->text().toUInt();  
  49.   
  50.     QRegExp qregexp( exp );  
  51.     int length = 0;  
  52.     int idx = qregexp.indexIn( text, 0 );  
  53.   
  54.     while( -1 != idx ) {  
  55.         int num = qregexp.numCaptures();  
  56.         QString str = qregexp.cap( index );  
  57.         if( !str.isEmpty() ) {  
  58.             QListWidgetItem *item = new QListWidgetItem( str );  
  59.             m_listResult->addItem( item );  
  60.         }  
  61.         length = qregexp.matchedLength();  
  62.         idx = qregexp.indexIn( text, idx + length );  
  63.     }  
  64. }  

main.cpp
[cpp] view plaincopy
  1. #include "regexp.h"  
  2. #include <QtGui/QApplication>  
  3. #include <QRegExp>  
  4. #include <QTextCodec>  
  5. #include <QObject>  
  6.   
  7. int main(int argc, char *argv[])  
  8. {  
  9.     QApplication a(argc, argv);  
  10.       
  11.     RegexpConner rc;  
  12.     rc.show();  
  13.   
  14.     return a.exec();  
  15. }  

以下是测试结果:








转载:http://blog.csdn.net/kusey/article/details/7383184


原创粉丝点击