正则表达式处理

QString   SysUtils :: formatHexString ( const   QString   & hex )
{
    QString hexStr(hex);
 
    QRegExp rx("([0-9A-Fa-f]{1,2})");
    QStringList list;
    int pos = 0;
 
    while ((pos = rx.indexIn(hexStr, pos)) != -1) {
        list << rx.cap(1);
        pos += rx.matchedLength();
    }
    return list.join(" ");
}
 
void MainWindow::HandleText()
{
    disconnect (ui->SendtextEdit,SIGNAL(textChanged()),this,SLOT(HandleText()));

QTextCursortextCursor=ui->SendtextEdit->textCursor();

    intrecordPos=textCursor.position();//记录光标位置

    QStringtxt=ui->SendtextEdit->toPlainText();//读取待处理字符串

    QStringtmp=txt;

    tmp=tmp.replace("","");//去掉待处理字符串空格

    QStringhexStr=SysUtils::formatHexString(tmp);

    ui->SendtextEdit->setText(hexStr);

    if(recordPos>0)

    {

        QStringch=hexStr.mid(recordPos-1,1);

        qDebug()<<ch;

        if(ch=="")

            recordPos++;

    }

    textCursor.setPosition(recordPos);

    ui->SendtextEdit->setTextCursor(textCursor);

connect (ui->SendtextEdit,SIGNAL(textChanged()),this,SLOT(HandleText()));

}

 

转自:http://blog.csdn.net/liuguangzhou123/article/details/8294332


正则表达式匹配

    QRegExp rx("[\u4e00-\u9fa5]"); 匹配utf-8格式的汉字,这个可以匹配一个,多个可以加入 +或者*
    QString str("我....这是神马");
    QStringList list;
    int pos = 0;
    while ((pos = rx.indexIn(str, pos)) != -1) //从pos位置开始匹配
	{
        list << rx.cap(0);  //cap(0)是整个匹配的,cap(1)是第一个括号中匹配的 cap(2)是第二个括号中匹配的
        pos += rx.matchedLength(); 范围开始匹配的位置  // rx.exactMatch(str) 返回是否正确
   	 }
    qDebug() << list;



int main(int argc, char *argv[])
{

QString str = "[(40,120),360x360]";
QRegExp rx("(\\d+)");
QStringList list ;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
list << rx.cap(0);
pos += rx.matchedLength();
}
//这只是为了写成4个数的格式

int rectx =  list.at(0).toInt();
int recty =  list.at(1).toInt();
int rectw =  list.at(2).toInt();
int recth =  list.at(3).toInt();

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值